Problems encountered when building JFinal:
1.jsp page has the following problems:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Solution: import the following packages
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1-jetty</artifactId>
<version>6.1.26</version>
</dependency>
2. Establish the mapping relationship between the database table name and the Model:
public void configPlugin(Plugins plugin) {
Prop p = PropKit.use("jdbc.properties");
//druid数据源插件
DruidPlugin dp = new DruidPlugin(p.get("url"), p.get("username"), p.get("password"));
plugin.add(dp);
//该方法建立了数据库表名到Model的映射关系
ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
arp.addMapping("user","id", User.class);
plugin.add(arp);
}
3. Get the project name:
public void configHandler(Handlers hand) {
//hand.add(new ResourceHandler());
hand.add(new ContextPathHandler("webctx"));
}
4. Configure project routing
/**
* 此方法用来配置访问路由如下代码配置了将 "/user" 映射到UserController这个控制器
* Routes.setBaseViewPath(baseViewPath)方法用于为该Routes 内部的所有Controller设置视图渲染时的基础路径
* finalView = baseViewPath + viewPath + view
* 注意:当view以 “/” 字符打头时表示绝对路径,baseViewPath 与 viewPath 将被忽略
*/
public void configRoute(Routes routes) {
routes.setBaseViewPath("/jsp");
routes.add("/user",UserController.class,"/user");
}
5. Start with jetty integrated with jfinal
import com.jfinal.core.JFinal;
public class DemoApplication {
public static void main(String[] args) {
/**
* 使用jfinal整合的jetty启动
*/
JFinal.start("src/main/webapp",8080,"/jdemo",5);
}
}
6.web.xml configuration:
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>Archetype Created Web Application</display-name>
<filter>
<filter-name>jfinal_demo</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>com.cn.demo.conf.DemoConfing</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal_demo</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
7. Project structure diagram: