JFinal簡介
JFinal 是基於Java語言的極速 WEB + ORM 開發框架,其核心設計目標是開發迅速、代碼量少、學習簡單、功能強大、輕量級、易擴展、Restful。在擁有 Java 語言所有優勢的同時 再擁有 ruby、python、php 等動態語言的開發效率!爲您節約更多時間,去陪戀人、家人和朋友。
JFinal特點
- MVC 架構,設計精巧,使用簡單
- 遵循 COC 原則,零配置,無 xml
- 獨創 Db + Record 模式,靈活便利
- ActiveRecord 支持,使數據庫開發極致快速
- 自動加載修改後的 java 文件,開發過程中無需重啓 web server
- AOP 支持,攔截器配置靈活,功能強大
- Plugin 體系結構,擴展性強
- 多視圖支持,支持 FreeMarker、JSP、Velocity
- 強大的 Validator 後端校驗功能
- 功能齊全,擁有 struts2 絕大部分核心功能
- 體積小僅 303K,且無第三方依賴
Jfinal之HelloWorld:
- 創建web項目
- 導入相關包
<dependency> <groupId>com.jfinal</groupId> <artifactId>jetty-server</artifactId> <version>8.1.8</version> <!-- 此處的 scope 值爲 compile 僅爲支持 IDEA 下啓動項目 打 war 包時需要改成 provided,以免將一些無用的 jar 打進去 --> <scope>compile</scope> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>3.3</version> </dependency>
- 寫配置類DemoConfig
import com.jfinal.config.*;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
public class DemoConfig extends JFinalConfig {
public void configConstant(Constants constants) {
constants.setDevMode(true);
constants.setEncoding("UTF-8");
constants.setViewType(ViewType.JSP);
}
public void configRoute(Routes routes) {
routes.add("/hello", DemoController.class);
}
public void configEngine(Engine engine) {
}
public void configPlugin(Plugins plugins) {
}
public void configInterceptor(Interceptors interceptors) {
}
public void configHandler(Handlers handlers) {
}
// 系統啓動完成後回調
public void afterJFinalStart() {
}
// 系統關閉之前回調
public void beforeJFinalStop() {
}
}
- 修改web.xml配置,將configClass改成你自己的config類
<?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"
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">
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>helloworld.DemoConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- 寫Controller
public class DemoController extends Controller {
public void index() {
renderText("hello world ,this is my first controller");
}
public void hello() {
renderText("this is hello method");
}
@ActionKey("/my/test1/key")
public void testKey() {
renderText("/my/test1/key");
}
}
- 啓動類
public class Starter extends Controller {
public static void main(String[] args) {
// 第二個參數爲端口
JFinal.start("src/main/webapp", 80, "/");
}
}
項目啓動瀏覽器輸入localhost:8080/hello,如下:
恭喜你,第一個Jfinal Demo搭建完成.