初识jfinal-部署到Tomcat

时间:2021-08-12 18:24:12

1。 使用MyEclipse创建一个基本的空白的Web项目,然后再按照jfinal提供的demo拷贝代码和配置。

涉及三个文件:DemoConfig.java,HelloController.java,Web.xml.

2。目录结构如下:采用的是jfinal提供的jar包,因为是使用tomcat作为服务器,就没有引入jetty-server-8.1.8.jar。

初识jfinal-部署到Tomcat

3。三个文件的内容分别是:

DemoConfig.java

 1 package com.demo;
2
3 import com.jfinal.config.*;
4 import com.jfinal.ext.handler.ContextPathHandler;
5 import com.jfinal.template.Engine;
6
7 public class DemoConfig extends JFinalConfig {
8 public void configConstant(Constants me){
9 me.setDevMode(true);
10 }
11 public void configRoute(Routes me) {
12
13
14 me.add("/hello", HelloController.class);
15
16 }
17 public void configEngine(Engine me) {}
18
19 public void configPlugin(Plugins me) {}
20
21 public void configInterceptor(Interceptors me) {}
22
23 public void configHandler(Handlers me) {
24 me.add(new ContextPathHandler());
25 }
26
27
28 }

 

HelloController.java

1 package com.demo;
2
3 import com.jfinal.core.Controller;
4
5 public class HelloController extends Controller{
6 public void index() {
7 renderText("Hello JFinal World.");
8 }
9 }

Web.xml.

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.5"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
7 <filter>
8
9 <filter-name>jfinal</filter-name>
10
11 <filter-class>com.jfinal.core.JFinalFilter</filter-class>
12
13 <init-param>
14
15
16 <param-name>configClass</param-name>
17
18 <param-value>com.demo.DemoConfig</param-value>
19
20 </init-param>
21 </filter>
22 <filter-mapping>
23
24 <filter-name>jfinal</filter-name>
25
26 <url-pattern>/*</url-pattern>
27 </filter-mapping>
28 </web-app>

4。部署到Tomvat,然后在浏览器输入:http://localhost:8080/creditProd/hello

初识jfinal-部署到Tomcat

成功展示信息。