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的绝大部分功能
- 体积小仅218K,且无第三方依赖
以下是JFinal实现Blog管理的示例:
1:控制器(支持FreeMarker、JSP、Velocity、JSON等等以及自定义视图渲染)
01 | @Before (BlogInterceptor. class ) |
02 | public class BlogController extends Controller { |
03 | public void index() { |
04 | setAttr( "blogList" , Blog.dao.find( "select * from blog" )); |
05 | } |
06 |
07 | public void add() { |
08 | } |
09 | |
10 | @Before (BlogValidator. class ) |
11 | public void save() { |
12 | getModel(Blog. class ).save(); |
13 | } |
14 | |
15 | public void edit() { |
16 | setAttr( "blog" , Blog.dao.findById(getParaToInt())); |
17 | } |
18 | |
19 | @Before (BlogValidator. class ) |
20 | public void update() { |
21 | getModel(Blog. class ).update(); |
22 | } |
23 | |
24 | public void delete() { |
25 | Blog.dao.deleteById(getParaToInt()); |
26 | } |
27 | } |
2:Model(无xml、无annotation、无attribute、无getter、无setter、new Blog()这行代码也不是必须)
1 | public class Blog extends Model { |
2 | public static final Blog dao = new Blog(); |
3 | } |
3:Validator(API引导式校验,比xml校验方便N倍,有代码检查不易出错)
01 | public class BlogValidator extends Validator { |
02 | protected void validate(Controller controller) { |
03 | validateRequiredString( "blog.title" , "titleMsg" , "请输入Blog标题!" ); |
04 | validateRequiredString( "blog.content" , "contentMsg" , "请输入Blog内容!" ); |
05 | } |
06 | |
07 | protected void handleError(Controller controller) { |
08 | controller.keepModel(Blog. class ); |
09 | } |
10 | } |
4:拦截器(在此demo中仅为示例,本demo不需要此拦截器)
1 | public class BlogInterceptor implements Interceptor { |
2 | public void intercept(ActionInvocation ai) { |
3 | System.out.println( "Before invoking " + ai.getActionKey()); |
4 | ai.invoke(); |
5 | System.out.println( "After invoking " + ai.getActionKey()); |
6 | } |
7 | } |