目录
建立方式
建立JFinal项目两种方式,一种是通过官方网站文档(https://jfinal.com/doc),另一个是使用JBolt(http://www.jbolt.cn)的eclipse插件。
使用JBolt插件可以快速生成一个带首页、有数据库配置的web项目。可以在创建前选择需要的内容,如数据库选择(支持Mysql/Oracle/Sql Server/H2/Sqlite/PostgreSql),server选择(支持undertow/jetty/tomcat/other),libs的选择(如hutool/Ehcache/POI/ZXing/Shiro/Jfinal-wexin等),但它不是一个平台,只是快速建立web项目的工具。
官方文档中如何建立项目说的很清楚,一步一步操作即可。只是官方是使用mac os下的eclipse,本次在windows下使用idea创建maven项目来进行操作。
创建之前
修改maven
本例中没有使用idea自带的maven,而是下载了apache的maven,并且修改了仓库的位置:D:\java\apache-maven-3.6.3\conf\settings.xml
指定本机的仓库位置
<localRepository>D:/Java/mavenrepo/repository</localRepository>
添加加速镜像
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
新建项目
自动生成了部分内容,需要修改pom.xml
修改pom.xml
<packaging>war</packaging>
改为
<packaging>jar</packaging>
修改编译环境为1.8
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
添加仓库
<!-- 使用阿里 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>
添加依赖
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal-undertow</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>4.9.06</version>
</dependency>
打包配置(自动生成plugns的内容可以删除替换成以下代码)
注意要将<pluginManagement>和</pluginManagement>删除掉,否则无法打成zip文件
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
<!-- java8 保留参数名编译参数 -->
<compilerArgument>-parameters</compilerArgument>
<compilerArguments>
<verbose/>
</compilerArguments>
</configuration>
</plugin>
<!-- jar 包中的配置文件优先级高于 config 目录下的 "同名文件" 因此,打包时需要排除掉 jar 包中来自 src/main/resources
目录的 配置文件,否则部署时 config 目录中的同名配置文件不会生效 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<excludes>
<exclude>*.txt</exclude>
<exclude>*.xml</exclude>
<exclude>*.properties</exclude>
</excludes>
</configuration>
</plugin>
<!-- 使用 mvn clean package 打包 更多配置可参考官司方文档:http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<!-- 打包生成的文件名 -->
<finalName>${project.artifactId}</finalName>
<!-- jar 等压缩文件在被打包进入 zip、tar.gz 时是否压缩,设置为 false 可加快打包速度 -->
<recompressZippedFiles>false</recompressZippedFiles>
<!-- 打包生成的文件是否要追加 release.xml 中定义的 id 值 -->
<appendAssemblyId>true</appendAssemblyId>
<!-- 指向打包描述文件 package.xml -->
<descriptors>
<descriptor>package.xml</descriptor>
</descriptors>
<!-- 打包结果输出的基础目录 -->
<outputDirectory>${project.build.directory}/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
需要在pom.xml同目录下建立package.xml
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!--
assembly 打包配置更多配置可参考官司方文档:
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
-->
<id>release</id>
<!--
设置打包格式,可同时设置多种格式,常用格式有:dir、zip、tar、tar.gz
dir 格式便于在本地测试打包结果
zip 格式便于 windows 系统下解压运行
tar、tar.gz 格式便于 linux 系统下解压运行
-->
<formats>
<format>dir</format>
<format>zip</format>
</formats>
<!-- 打 zip 设置为 true 时,会在 zip 包中生成一个根目录,打 dir 时设置为 false 少层目录 -->
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!-- src/main/resources 全部 copy 到 config 目录下 -->
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
<!-- src/main/webapp 全部 copy 到 webapp 目录下 -->
<fileSet>
<directory>${basedir}/src/main/webapp</directory>
<outputDirectory>webapp</outputDirectory>
<excludes>
<exclude>WEB-INF</exclude>
<exclude>WEB-INF/web.xml</exclude>
</excludes>
</fileSet>
<!-- 项目根下面的脚本文件 copy 到根目录下 -->
<fileSet>
<directory>${basedir}</directory>
<lineEnding>unix</lineEnding>
<outputDirectory></outputDirectory>
<!-- 脚本文件在 linux下的权限设为755,无需chmod可直接运行 -->
<fileMode>755</fileMode>
<includes>
<include>*.sh</include>
</includes>
</fileSet>
<fileSet>
<directory>${basedir}</directory>
<lineEnding>windows</lineEnding>
<outputDirectory></outputDirectory>
<fileMode>755</fileMode>
<includes>
<include>*.bat</include>
</includes>
</fileSet>
</fileSets>
<!-- 依赖的 jar 包 copy 到 lib 目录下 -->
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
重新加载maven的改变
建立代码文件夹
在main目录下建立java和resources
建立common包
可以取消包的合并
建立配置类AppConfig.java
继承JFinalConfig,导入相关methods
源码:
package cc.onthis.common;
import com.jfinal.config.*;
import com.jfinal.template.Engine;
public class AppConfig extends JFinalConfig {
@Override
public void configConstant(Constants me) {
// true=开发模式
me.setDevMode(true);
}
@Override
public void configRoute(Routes me) {
// 如果要将控制器超类中的 public 方法映射为 action 配置成 true,一般不用配置
me.setMappingSuperClass(false);
// 配置 baseViewPath,可以让 render(...) 参数省去 baseViewPath 这部分前缀
me.setBaseViewPath("/view");
// 使用路由扫描,参数 "cc.onthis." 表示只扫描 demo 包及其子包下的路由
me.scan("cc.onthis.");
}
@Override
public void configEngine(Engine me) {
}
@Override
public void configPlugin(Plugins me) {
}
@Override
public void configInterceptor(Interceptors me) {
}
@Override
public void configHandler(Handlers me) {
}
}
创建启动类Application.java
public static void main(String[] args) {
UndertowServer.create(AppConfig.class,"undertow.properties").start();
}
AppConfig.class是刚才建立的配置类
undertow.properties是配置文件,需要在resources中建立该文件
建立启动配置文件undertow.properties
# 常用配置
# true 值支持热加载 生产环境建议配置成 false
undertow.devMode=true
# 项目端口
undertow.port=80
undertow.host=0.0.0.0
# 绝大部分情况不建议配置 context path
#undertow.contextPath=/
# web 资源加载路径配置
undertow.resourcePath = src/main/webapp, classpath:webapp
# 开启 gzip 压缩
undertow.gzip.enable=true
# 配置压缩级别,默认值 -1。 可配置 1 到 9。 1 拥有最快压缩速度,9 拥有最高压缩率
undertow.gzip.level=-1
# 触发压缩的最小内容长度
undertow.gzip.minLength=1024
建立首页
创建包与文件
启动项目
打开Application.java,右键选择debug
显示如下即为成功
打开浏览器,录入http://localhost