本篇介紹JFinal
項目的搭建和簡單功能實現。
JFinal項目搭建
項目搭建
新建一個Dynamic Web Project
Target runtime
為
Default Output Folder
推薦使用WebRoot\WEB-INF\classes
*此處的 Default out folder
必須要與 WebRoot\WEB-INF\classes
目錄完全一致才可以使用 JFinal
集成的 Jetty
來啟動項目。 *
修改 Content directory
,推薦輸入WebRoot
。
jfinal-xxx.jar
與 jetty-server-8.1.8.jar
拷貝至項目 WEB-INF\lib
下即可。
- Tips:這里的
WEB-INF\lib
為WebRoot
目錄下的和上面配置的保持一致,當然你也可以使用WebContent
修改 web.xml
:
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>config.DemoConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
添加DemoConfig
:
package config;
import com.jfinal.config.*;
import controller.HelloController;
public class DemoConfig extends JFinalConfig {
// 配置常量值 `如開發模式常量 devMode 的配置,默認視圖類型 ViewType的配置`
public void configConstant(Constants me) {
me.setDevMode(true);
}
// 配置 JFinal 訪問路由 如下代碼配置了將”/hello”映射到 HelloController 這個控制器 http://localhost/hello 將 訪 問 HelloController.index() 方
public void configRoute(Routes me) {
me.add("/hello", HelloController.class);
}
// 配置 JFinal 的 Plugin 如數據庫訪問插件
public void configPlugin(Plugins me) {
}
// 配置 JFinal 的全局攔截器
public void configInterceptor(Interceptors me) {
}
// 配置JFinal的Handler
public void configHandler(Handlers me) {
}
}
添加HelloController
:
package controller;
import com.jfinal.core.Controller;
public class HelloController extends Controller {
public void index() {
renderText("Hello JFinal World.");
}
}
Run As --> Run configurations
- 訪問http://localhost/hello
上面的啟動配置也可以使用一個任意的main方法代替。在任意一個類文件中添加一個main啟動集成的jetty
如在DemoConfig
中:
public static void main(String[] args) {
// eclipse 下的啟動方式 指定端口和路徑
JFinal.start("WebRoot", 8088, "/Demo", 5);
}
連接mysql數據庫
修改DemoConfig
:
//加載datasource.properties 數據庫配置
public void configConstant(Constants me) {
loadPropertyFile("datasource.properties");
me.setEncoding("UTF-8");
me.setDevMode(true);
}
//連接mysql數據庫
public void configPlugin(Plugins me) {
C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
me.add(c3p0Plugin);
ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
me.add(arp);
}
//更改啟動項目配置 現在就可以直接 `Run As` -->`Java Appliaction` 訪問地址`http://localhost:8088/Demo/index.html`
public static void main(String[] args) throws Exception {
JFinal.start("WebRoot", 8088, "/Demo", 5);
}
添加datasource.properties
:
jdbcUrl = jdbc:mysql://121.40.78.44/zhitong_test?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
user = root
password =Ne0Print1202
devMode = true
簡單的增刪查改功能
下面簡單實現賬號的增刪查改。(不要太在意邏輯...)
添加一個簡單的html頁面,index.html
hello/addUser
、hello/deleteUser
、hello/findUser
和hello/updateUser
分別對應着HelloController
的四個方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<span>賬號</span>
<input type="text" id="username" />
<span>密碼</span>
<input type="text" id="password" />
<button id="add">添加</button>
<button id="delete">刪除</button>
<button id="find">查詢</button>
<button id="update">修改</button>
</body>
<script type="text/javascript" src="js/jquery-1.9.1.js" ></script>
<script>
$(document).ready(function(){
// 點擊添加調用 HelloController中的addUser方法,路徑"hello/addUser"
// 路徑在 DemoConfig - configRoute 已配置好為 /hello
// 賬號添加 傳入參數 ”賬號密碼”
$("#add").click(function(){
var _userName = $("#username").val();
var _password = $("#password").val();
$.post("hello/addUser",{userName:_userName,password:_password},function(data){
alert(data);
})
});
// 賬號刪除 傳入參數 ”賬號密碼”
$("#delete").click(function(){
var _userName = $("#username").val();
var _password = $("#password").val();
$.post("hello/deleteUser",{userName:_userName,password:_password},function(data){
alert(data);
})
});
// 密碼查詢 傳入參數 ”賬號” 根據賬號 查找密碼
$("#find").click(function(){
var _userName = $("#username").val();
$.post("hello/findUser",{userName:_userName},function(data){
alert(data);
})
});
// 密碼修改 傳入參數 ”賬號 新密碼” 根據賬號 修改密碼
$("#update").click(function(){
var _userName = $("#username").val();
var _password = $("#password").val();
$.post("hello/updateUser",{userName:_userName,password:_password},function(data){
alert(data);
})
});
})
</script>
</html>
在service
包中添加和HelloController
對應的HelloService
這里使用
JFinal
獨創的Db + Record
模式 ,提供了Model
類之外的數據庫操作功能。它充當了MVC
中的Model
層。
package service;
import java.util.List;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
public class HelloService {
public static void addUser(Record record){
Db.save("zt_user", record);
}
public static void deleteUser(String userName,String password){
Db.update("delete from zt_user where name = "+userName+" and password = "+password+" ");
}
public static Record findUser(String userName){
return Db.findFirst("select password from zt_user where name = "+userName+" ");
}
public static void updateUser(String userName,String password){
Db.update("update zt_user set password = "+password+" where name = "+userName+" ");
}
}
然后修改HelloController
jfinal Controller類提供了getPara系列方法用來從請求中獲取參數。
這里我們使用getPara((String string)來獲取賬號和密碼。
import java.util.List;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import service.HelloService;
import com.jfinal.core.Controller;
public class HelloController extends Controller {
public void addUser() {
String userName = getPara("userName");
String password = getPara("password");
Record user = new Record().set("name", userName).set("password", password);
HelloService.addUser(user);
renderText("添加成功");
}
public void deleteUser() {
String userName = getPara("userName");
String password = getPara("password");
HelloService.deleteUser(userName, password);
renderText("刪除成功");
}
public void findUser() {
String userName = getPara("userName");
Record record = HelloService.findUser(userName);
renderText(""+record.get("password")+"");
}
public void updateUser() {
String userName = getPara("userName");
String password = getPara("password");
HelloService.updateUser(userName, password);
renderText("修改成功");
}
}
other
同樣你也可以使用Model
類對應configPlugin
配置 。
DemoConfig
:
arp.addMapping("user", User.class);
建立數據庫表名到 Model 的映射關系,然后使用繼承Model的方法。
public class User extends Model<User> {
public static final User dao = new User();
}
項目部署
1.1 Jfinal
項目部署和web項目相同,將項目webroot下的文件(包括webroot)拷貝到服務器tomcat
的webapps目錄下面。
2.2 然后刪除webroot中的 jetty-server-8.1.8.jar
jar包
3.3 startup
運行tomcat
即可
Tips: 項目JDK版本
要和服務器JDK
版本一致,高版本編譯的項目不能跑在低版本上面。
Tips: 使用render html
404
,可以使用redirect
1:redirect 是重定向,當服務端向客戶端響應 redirect后,並沒有提供任何view數據進行渲染,僅僅是告訴瀏覽器響應為 redirect,以及重定向的目標地址
2:瀏覽器收到服務端 redirect 過來的響應,會再次發起一個 http 請求
3:由於是瀏覽器再次發起了一個新的 http 請求,所以瀏覽器地址欄中的 url 會發生變化
4:瀏覽中最終得到的頁面是最后這個 redirect url 請求后的頁面
5:所以redirect("/user/login.html") 相當於你在瀏覽器中手動輸入 localhost/user/login.html