不灭的焱

革命尚未成功,同志仍须努力

作者:php-note.com  发布于:2018-12-08 16:58  分类:电脑/软件使用  编辑

一、安装 IDEA、Maven、Tomcat,参考链接:

下载IDEAUbuntu 14 安装Java(JRE、JDK)、Maven使用阿里云Maven仓库Linux下安装和配置JDK与Tomcat

二、配置 IDEA

如果要 更新  java 文件,按快捷键 F5,如果要更新 非 Java 文件(如:resoutces ),按快捷键 F4

详细参考:IDEA + Tomcat 实现Java代码自动(快速)热更新

三、新建 Java Web 项目

1、File -> New -> Project -> Maven -> 勾选“Create from archetype”-> 选择“...maven-archetype-webapp”

 

2、默认情况下,没有java源码目录 和 配置文件目录,自己新建一下:

/src/main/java

/src/main/resources

选中目录/src/main/java,右键选择 Mark Directory as -> Sources Root

选中目录/src/main/resources,右键选择 Mark Directory as -> Resources Root

 

3、配置 pom.xml,增加对 JFinal 框架的依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>jianbao.blog</groupId>
    <artifactId>jianbao-blog</artifactId>
    <version>1.0.0</version>

    <name>jianbao-blog</name>
    <url>http://php-note.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <jfinal.version>3.5</jfinal.version>
    </properties>

    <!-- 使用阿里 maven 库 -->
    <repositories>
        <repository>
            <id>ali-maven</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->

        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>jfinal</artifactId>
            <version>${jfinal.version}</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>jianbao-blog</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>

                <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
                <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
                <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
                
            </plugins>
        </pluginManagement>
    </build>
</project>

 

4、新建 控制器文件:/src/main/controller/UserController.java

package controller;

import com.jfinal.core.Controller;

public class UserController extends Controller {
    public void index() {
        renderText("欢迎来到 JFinal世界!");
    }
}

 

5、新建 配置文件:/src/main/java/config/BlogConfig.java

JFinal需要有一个配置文件,所有的配置信息都可以在该文件中进行配置,该配置文件需要继承JFinalConfig

package config;

import com.jfinal.config.*;
import com.jfinal.template.Engine;
import controller.UserController;

public class BlogConfig extends JFinalConfig {
    @Override
    public void configConstant(Constants me) {
        me.setDevMode(true);
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/", UserController.class);
    }

    @Override
    public void configEngine(Engine me) {
    }

    @Override
    public void configPlugin(Plugins me) {
    }

    @Override
    public void configInterceptor(Interceptors me) {
    }

    @Override
    public void configHandler(Handlers me) {
    }
}

 

6、配置 过滤器(关键步骤):/src/main/webapp/WEB-INF/web.xml

在web.xml文件中配置过滤器,web项目启动后需要根据这个过滤器来找到配置文件读取相关配置信息

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>jiabao-blog</display-name>
    
    <filter>
        <filter-name>jfinal</filter-name>
        <filter-class>com.jfinal.core.JFinalFilter</filter-class>
        <init-param>
            <param-name>configClass</param-name>
            <param-value>config.BlogConfig</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>jfinal</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

7、配置项目编译输出目录

8、新建 项目的 Web 特征(Facets)

File -> Project Structure -> Project Settings -> Facets ->“+”-> Web -> Choose Module -> 选择“jianbao-blog”

 

9、新建 项目的 发布成品(Artifacts)

File -> Project Structure -> Project Settings -> Artifacts ->“+”-> Web Application:Exploded -> Select Modules -> 选择“jianbao-blog”

 

10、配置 Tomcat

Run -> Editor Configurations -> 弹框 -> 左上角“+” -> Tomcat Server -> Local

 

11、调试网站

特别注意,是“调试”网站,而不是“运行”网站,因为只有在“调试”模式下,IDEA才支持 Java 文件被修过后,热编译更新(不需要重启tomcat)