Jfinal初体验(一)

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:

  1. 创建web项目
  2. 导入相关包
    <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>
    
  3. 写配置类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() {
    }

}
  1. 修改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>
  1. 写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");
    }
}
  1. 启动类
public class Starter extends Controller {
    public static void main(String[] args) {
        // 第二个参数为端口
        JFinal.start("src/main/webapp", 80, "/");
    }
}

项目启动浏览器输入localhost:8080/hello,如下:
在这里插入图片描述
恭喜你,第一个Jfinal Demo搭建完成.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章