首先呢,还是要去下载JFinal的手册,这里是用的是3.1版本的
jFinal3.1快速下载入口:https://download.csdn.net/download/qq_35823302/10494585
当然了,这里我用的是Maven环境的,主要的是..可以看源码 ps: Maven 还是挺好用的。
搭建Maven环境链接:http://blog.csdn.net/zhshulin/article/details/37921705
准备好了就直接开始
第一步:环境配置
我用的是JDK是1.7版本,编码格式是:UTF-8,tomcat是8.0
第二步:导入包
(我不会直接把包复制下来,用到什么包就添加什么包)
在maven项目中的pom.xml文件中添加JFinal包
<!-- jfinal 包 -->
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>3.1</version>
</dependency>
第三步:建包
包的格式是这样的(凭个人喜好)
第四步:配置核心配置文件、web.xml、controller
添加baseConfig类,这个类是整个JFinal的核心配置文件,继承 JFinalConfig 然后实现它的方法,并在 configConstant中设置基本配置,具体还有其他配置可在文档中搜索 configConstant
添加一个controller 用来访问 jsp页面。
其中UserController 继承 JFinal中的 Controller类 并添加访问action
package com.gx.test.web.controller;
import com.jfinal.core.Controller;
/**
* 用户控制器
* @title
* @filename UserController.java
* @author L-z
* @date 2017年8月2日 下午8:53:50
* @version 1.0
*/
public class UserController extends Controller{
/**
* 显示视图 注意:controller中的action 都是public修饰,并且都是无参的方法
* index
* 2017年8月2日 下午8:59:30
*/
public void index(){
//返回视图
super.render("/WEB-INF/jsp/hello.jsp");
}
}
完成后再到核心配置文件baseConfig类 configRoute 方法 中配置访问路径(官方称为配置路由)
/**
* 配置路由,用于控制器的访问
*/
@Override
public void configRoute(Routes routes) {
routes.add("/user",UserController.class);
System.out.println("-----------------------路由加载成功---------------------------------------");
}
还要配置web.xml中的内容(filter - -不就是过滤器么,很多框架都是以这个为入口的)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>jfinal</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<!-- 这里的类是我们创建的核心配置文件 -->
<param-value>com.gx.test.core.base.BaseConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
最后用tomcat8启动看看 访问路径:http://localhost:8080/JFinalTest/user/index 如果出现jsp中的内容 说明 JFinal 框架搭建成功