介紹開始:
編輯器:MyEclipse;
數據庫:MySQL;
服務器:tomcat;
1 首先新建web項目
要強調的是Target runtime必須選擇爲None。而後點擊兩次Next,選中建立web.xml。finish~~~css
2 導入支撐項目的jar包(包括JFinal、Mysql、JDBC)
必須導入這四個jar包。其中:(1) c3p0是支撐JDBC的;(2)jfina是支撐jfinal的,jfinal默認使用freemarker做爲MVC的View,因此也要導入;(3)mysql-conncetor是支撐Mysql鏈接的html
3 寫java代碼
在src文件夾中建立四個package,分別爲config,controller, model,service。其中config包裏是全局配置文件,controller對應的就是MVC中的Control,model與service組成MVC的Model。Control就是控制先後臺的交互,model對應數據庫的各個表以及派生model類,service就是寫sql語句的地方。java
config中有三個方法最重要configConstant、configPlugin、configRoutemysql
(1)configConstant:此方法用來配置 JFinal 常量值web
(2)configPlugin:此方法用來配置 JFinal 的 Plugin,好比配置 C3p0 數據庫鏈接池插件與 ActiveRecord數據庫訪問插件sql
(3)configRoute:此方法用來配置 JFinal 訪問路由,好比將」/hello」映射到 HelloController 這個控制器數據庫
說完這些咱們來看代碼tomcat
↓此爲BaseConfig.java服務器
- package config;
-
- import model.User;
-
- import com.jfinal.config.Constants;
- import com.jfinal.config.Handlers;
- import com.jfinal.config.Interceptors;
- import com.jfinal.config.JFinalConfig;
- import com.jfinal.config.Plugins;
- import com.jfinal.config.Routes;
- import com.jfinal.kit.PropKit;
- import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
- import com.jfinal.plugin.c3p0.C3p0Plugin;
-
- import controller.HelloController;
-
- public class BaseConfig extends JFinalConfig {
-
- @Override
- public void configConstant(Constants me) {
-
- PropKit.use("demo_config.txt");
- me.setDevMode(PropKit.getBoolean("devMode", false));
- }
- @Override
- public void configHandler(Handlers me) {
-
-
- }
- @Override
- public void configInterceptor(Interceptors me) {
-
-
- }
- @Override
- public void configPlugin(Plugins me) {
-
- C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
- me.add(c3p0Plugin);
-
- ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
- me.add(arp);
-
- arp.addMapping("user", User.class);
- }
- @Override
- public void configRoute(Routes me) {
-
- me.add("/hello", HelloController.class);
- }
- }
↓此是HelloController.javaapp
- package controller;
-
- import model.User;
- import service.UserService;
-
- import com.jfinal.core.Controller;
-
- public class HelloController extends Controller {
-
- UserService userService = new UserService();
-
- public void index()
- {
- String name = getPara("name");
- String password = getPara("password");
- User user = new User();
- user = null;
-
- user = userService.findUser(name, password);
-
- if(user != null)
- render("/html/hello.html");
-
-
- }
- }
↓此是User.java
- package model;
-
- import com.jfinal.plugin.activerecord.Model;
-
- public class User extends Model<User> {
- public static final User dao = new User();
- }
↓此是UserService.java
- import model.User;
-
- public class UserService {
-
- public User findUser(String name,String password)
- {
- User user = new User();
- user = null;
-
- String sql = "select * from user where name='"+name+"' and password='"+password+"';";
-
- user = User.dao.findFirst(sql);
-
- System.out.printf("sql:"+sql);
-
- return user;
- }
- }
4 千萬不能忘了Propkit要引用的txt文件
Propkit是JFinal自帶的工具類。Propkit能夠用來加載一個或多個配置文件。在該Demo中PropKit用在BaseConfig的configConstant方法中。
須要強調的是,加載的配置文件必需要放在src下。緣由是:友情連接http://www.oschina.net/question/2461549_2148022?fromerr=ceMjBkEb
↓此是demo_config.txt
- jdbcUrl = jdbc:mysql://127.0.0.1/jfinal_test?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
- user = root
- password = root
- devMode = true
5 寫前臺界面
在WebRoot下建立文件夾html,在html文件夾下建立hello.html與login.html。其中hello.html裏什麼都不用添加,login.html設計一個表單提交。
↓此是login.html
- <!DOCTYPE html>
- <html>
- <head>
- <title>login.html</title>
-
- <meta name="keywords" content="keyword1,keyword2,keyword3">
- <meta name="description" content="this is my page">
- <meta name="content-type" content="text/html; charset=UTF-8">
-
-
-
- </head>
-
- <body>
- <form action="../hello" method="post">
- <table>
- <tr>
- <td>name:</td>
- <td><input type="text" name="name"></td>
- </tr>
- <tr>
- <td>password:</td>
- <td><input type="password" name="password"></td>
- </tr>
- <tr>
- <td></td>
- <td><input type="submit" value="Login"></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
6 修改web.xml文件
web.xml在WebRoot/WEB-INF文件夾下
↓此是web.xml
- <?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_3_0.xsd" id="WebApp_ID" version="3.0">
- <display-name>JFinalDemo</display-name>
- <filter>
- <filter-name>JFinalDemo</filter-name>
- <filter-class>com.jfinal.core.JFinalFilter</filter-class>
- <init-param>
- <param-name>configClass</param-name>
- <param-value>config.BaseConfig</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>JFinalDemo</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
7 數據庫
建立數據庫的語句與插入的測試數據。
- CREATE DATABASE IF NOT EXISTS jfinal_test DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
- CREATE TABLE user (id INT NOT NULL AUTO_INCREMENT,name CHAR(10) NOT NULL,password CHAR(10) NOT NULL,PRIMARY KEY(id));
- insert into user(name,password) values('test','test');
8 執行
帳戶密碼都是test,點擊login後,跳轉成功
總結
因爲本人並無structs經驗,MVC也只是入門級,因此剛開始閱讀JFinal的文檔時徹底不會配置,可是認真梳理一下,從頭開始一步步的導入支撐的文件,並結合本身的MVC體會,算是會用JFinal了。在後面的一段時間內,我會常常與JFinal打交道,因此之後還會寫一些對JFinal的使用感悟。