一、開發環境
1、windows 7 企業版
2、Eclipse IDE for Enterprise Java Developers Version: 2019-03 (4.11.0)
3、JDK 1.8
4、Maven 3.5.2
5、MariaDB
6、Tomcat 8.5
二、基礎配置
1、Eclipse中Maven的設定如下圖
2、資料庫使用預設的test庫,建立表category1 CREATE TABLE category
2 (
3 categoryid INT AUTO_INCREMENT PRIMARY KEY,
4 categoryname VARCHAR(10) NOT NULL
5 );
6
7 INSERT INTO category VALUES(NULL, '圖書'), (NULL, '美妝');
8
9 SELECT * FROM category;
三、環境搭建
1、建立Maven工程
2、使用預設工作空間
3、選擇工程初始類型webapp
4、填寫包名和工程名
5、點選Finish,開始建立Maven工程。建立完畢後,會提示找不到Servlet包的錯誤。可以通過導入Tomcat解決該問題。
删除預設的index.jsp檔案,後面我們使用html頁面。在webapp目錄的WEB-INF目錄下建立classes目錄。最終的目錄結構如下
6、編寫pom.xml檔案
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>cn.temptation</groupId>
6 <artifactId>jfinalDemo</artifactId>
7 <packaging>war</packaging>
8 <version>0.0.1-SNAPSHOT</version>
9 <name>jfinalDemo Maven Webapp</name>
10 <url>http://maven.apache.org</url>
11 <dependencies>
12 <dependency>
13 <groupId>junit</groupId>
14 <artifactId>junit</artifactId>
15 <version>3.8.1</version>
16 <scope>test</scope>
17 </dependency>
18 <!-- 添加jfinal依賴 -->
19 <dependency>
20 <groupId>com.jfinal</groupId>
21 <artifactId>jfinal</artifactId>
22 <version>3.8</version>
23 </dependency>
24 <!-- 添加jetty server依賴 -->
25 <dependency>
26 <groupId>com.jfinal</groupId>
27 <artifactId>jetty-server</artifactId>
28 <version>2019.3</version>
29 <!-- 使用IDEA開發時,scope設定成為compile,否則提示缺少jar包,打包時記得要改回 provided -->
30 <scope>provided</scope>
31 </dependency>
32 <!-- mysql驅動 -->
33 <dependency>
34 <groupId>mysql</groupId>
35 <artifactId>mysql-connector-java</artifactId>
36 <version>5.1.37</version>
37 </dependency>
38 <!-- druid連接配接池 -->
39 <dependency>
40 <groupId>com.alibaba</groupId>
41 <artifactId>druid</artifactId>
42 <version>1.0.29</version>
43 </dependency>
44 </dependencies>
45 <build>
46 <finalName>jfinalDemo</finalName>
47 </build>
48 </project>
7、編寫web.xml配置檔案,主要就是設定一下過濾器
1 <!DOCTYPE web-app PUBLIC
2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3 "http://java.sun.com/dtd/web-app_2_3.dtd" >
4
5 <web-app>
6 <filter>
7 <filter-name>jFinal</filter-name>
8 <filter-class>com.jfinal.core.JFinalFilter</filter-class>
9 <init-param>
10 <param-name>configClass</param-name>
11 <param-value>cn.temptation.config.MyConfig</param-value>
12 </init-param>
13 </filter>
14 <filter-mapping>
15 <filter-name>jFinal</filter-name>
16 <url-pattern>/*</url-pattern>
17 </filter-mapping>
18 </web-app>
8、編寫資料庫連接配接配置檔案db.properties,為了能在架構中讀取到該配置檔案内容,把該檔案放在前面建立的webapp目錄的WEB-INF目錄下的classes目錄中。
1 jdbcUrl = jdbc:mysql://localhost/test?characterEncoding=utf8
2 user = root
3 password = sa
4 devMode = true
5 showSql = true
9、編寫實體類Category,因為要使用ActiveRecord,是以從com.jfinal.plugin.activerecord.Model繼承
1 package cn.temptation.bean;
2
3 import com.jfinal.plugin.activerecord.Model;
4
5 @SuppressWarnings("serial")
6 public class Category extends Model<Category> {
7
8 }
10、編寫配置類MyConfig
1 package cn.temptation.config;
2
3 import com.jfinal.config.Constants;
4 import com.jfinal.config.Handlers;
5 import com.jfinal.config.Interceptors;
6 import com.jfinal.config.JFinalConfig;
7 import com.jfinal.config.Plugins;
8 import com.jfinal.config.Routes;
9 import com.jfinal.core.JFinal;
10 import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
11 import com.jfinal.plugin.druid.DruidPlugin;
12 import com.jfinal.template.Engine;
13
14 import cn.temptation.bean.Category;
15 import cn.temptation.web.CategoryController;
16
17 public class MyConfig extends JFinalConfig {
18 /**
19 * 配置常量
20 */
21 @Override
22 public void configConstant(Constants me) {
23 // 加載資料庫配置檔案
24 loadPropertyFile("db.properties");
25 me.setDevMode(true);
26 // 開啟支援注解,支援 Controller、Interceptor 之中使用 @Inject 注入業務層,并且自動實作 AOP
27 me.setInjectDependency(true);
28 }
29
30 @Override
31 public void configRoute(Routes me) {
32 me.add("/", CategoryController.class);
33 }
34
35 /**
36 * 配置插件
37 */
38 @Override
39 public void configPlugin(Plugins me) {
40 // 配置druid連接配接池
41 DruidPlugin db = new DruidPlugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"));
42 me.add(db);
43 // ActiveRecord是作為JFinal的Plugin而存在的,是以使用時需要在JFinalConfig中配置ActiveRecordPlugin
44 ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(db);
45 activeRecordPlugin.addMapping("category", Category.class);
46 // 展示sql語句
47 activeRecordPlugin.setShowSql(true);
48 me.add(activeRecordPlugin);
49 }
50
51 /**
52 * 配置全局攔截器
53 */
54 @Override
55 public void configInterceptor(Interceptors me) {
56
57 }
58
59 /**
60 * 配置處理器
61 */
62 @Override
63 public void configHandler(Handlers me) {
64
65 }
66
67 /**
68 * 配置模闆
69 */
70 @Override
71 public void configEngine(Engine me) {
72
73 }
74
75 /**
76 * 啟動方法
77 *
78 * @param args
79 */
80 public static void main(String[] args) {
81 JFinal.start("src/main/webapp", 8080, "/", 5);
82 }
83 }
11、編寫控制器CategoryController,從com.jfinal.core.Controller繼承
1 package cn.temptation.web;
2
3 import java.util.List;
4
5 import com.jfinal.core.Controller;
6
7 import cn.temptation.bean.Category;
8
9 public class CategoryController extends Controller {
10 private Category categoryDao = new Category().dao();
11
12 public void index() {
13 List<Category> categories = categoryDao.find("SELECT * FROM category");
14 setAttr("categories", categories);
15 render("category.html");
16 }
17 }
12、在webapp目錄下建立category.html,将服務端擷取到的類别資料循環取出呈現
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 <title>類别清單</title>
6 <style>
7 .tblCategory {
8 border: 1px solid black;
9 border-collapse: collapse;
10 }
11 </style>
12 </head>
13 <body>
14 <table border="1" class="tblCategory">
15 <tr>
16 <th width="100px">類别編号</th>
17 <th width="150px">類别名稱</th>
18 </tr>
19 #for(category:categories)
20 <tr>
21 <td>#(category.categoryid)</td>
22 <td>#(category.categoryname)</td>
23 </tr>
24 #end
25 </table>
26 </body>
27 </html>
13、啟動程式,在MyConfig類中直接運作(ctrl + F11)或調試程式(F11),一切順利的話,可以看到如下内容。這是整合了jetty作為web容器的玩法
14、 整個工程的目錄結構
15、要釋出成war包,丢到tomcat中運作也很簡單,隻要在工程上右鍵找到Run AS----->Maven build,輸入clean package,再點選Run,即可打war包。把生成好的war包丢到tomcat的webapps目錄下,啟動tomcat運作吧,注意,tomcat下通路的路徑預設是要帶上該工程的目錄的,比如本例子中通路的路徑就應該是http://localhost:8080/jfinalDemo/