idea 2019 1.2 基于 jfinal 创建 maven 项目+tomcat 方式启动
maven 项目的创建可看小飞之前的博客,本文就不再介绍。
传送门:https://blog.csdn.net/acsiont/article/details/99695038
1、目录结构
2、 基于 jfinal 启动
2.1 添加 jfinal 依赖
- 在dependencies 标签中添加下列代码
<!-- jfinal 依赖-->
<dependencies>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal-undertow</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>4.4</version>
</dependency>
</dependencies>
2.2 配置所需文件
- 项目 src/main/java/config ,在 config 包下创建 Config.java 文件。 内容如下
package config;
import com.jfinal.config.*;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
public class Config extends JFinalConfig {
// 启动jfinal 函数
public static void main(String[] args) {
UndertowServer.start(Config.class, 80, true);
}
// 常量配置
public void configConstant(Constants me) {
me.setDevMode(true);
}
// 路由配置
public void configRoute(Routes me) {
}
public void configEngine(Engine me) {}
public void configPlugin(Plugins me) {}
public void configInterceptor(Interceptors me) {}
public void configHandler(Handlers me) {}
}
- 项目 src/main/java/controller,在 controller 包下创建 HelloController 类文件, 内容如下:
package controller;
import com.jfinal.core.Controller;
public class HelloController extends Controller {
public void index() {
renderText("Hello JFinal World.");
}
}
- 修改 Config.java 配置访问url
// 路由配置
public void configRoute(Routes me) {
me.add("/hello", HelloController.class);
}
2.3 jfinal 启动 完整 pom.xml代码
<?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>com.acsiont</groupId>
<artifactId>jfinal_tomcat</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>jfinal_tomcat Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- jfinal 依赖-->
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal-undertow</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>4.4</version>
</dependency>
</dependencies>
</project>
2.4 启动 jfinal
- 项目 src/main/java/config 找到Config.java类 右击Debug
- 或者点击main 左边的三角符号
- 出现下图提示就成功啦~
2.5访问测试
- 打开浏览器输入 http:/127.0.0.1/hello 或者 localhost/hello
- 看到如下结果就表示成功!
3、jfinal 基于 tomcat 启动
3.1 修改web.xml文件
- 项目 src/main/webapp/WEB-INF,在 web.xml 修改 内容如下:
<!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>Archetype Created Web Application</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.Config</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.2 添加 servlet api 依赖
- 在pom.xml 下添加下述代码
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
3.3 添加tomcat7 插件
- 在pom.xml 下添加下述代码
<build>
<finalName>base</finalName>
<plugins>
<plugin>
<!--maven对应的tomcat插件,启动tomcat的时候使用-->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<path>/</path>
<uriEncoding>utf-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
3.4 tomcat 启动 jfinal 完整 pom.xml
<?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>com.acsiont</groupId>
<artifactId>jfinal_tomcat</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>jfinal_tomcat Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- servlet 依赖->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- jfinal 依赖-->
<!-- <dependency>-->
<!-- <groupId>com.jfinal</groupId>-->
<!-- <artifactId>jfinal-undertow</artifactId>-->
<!-- <version>1.9</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>4.4</version>
</dependency>
</dependencies>
<build>
<finalName>base</finalName>
<plugins>
<plugin>
<!--maven对应的tomcat插件,启动tomcat的时候使用-->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<path>/</path>
<uriEncoding>utf-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.5 启动jfinal
- 将项目 src/main/java/config 找到Config.java类 中的 main 函数注释
- 将 pom.xml 中 jfinal-undertow 依赖注释
- 启动
- 启动结果 如下图就成功啦
3.6 访问测试
- 打开浏览器输入 http:/127.0.0.1/hello 或者 localhost/hello
- 看到如下结果就表示成功!