每日博客
JFinal有如下主要特点:
MVC架构,设计精巧,使用简单
遵循COC原则,支持零配置,无xml
独创Db + Record模式,灵活便利
ActiveRecord支持,使数据库开发极致快速
自动加载修改后的java文件,开发过程中无需重启web server
AOP支持,拦截器配置灵活,功能强大
Plugin体系结构,扩展性强
多视图支持,支持FreeMarker、JSP、Velocity
强大的Validator后端校验功能
功能齐全,拥有struts2的绝大部分功能
体积小仅 723 KB,且无第三方依赖
控制器(支持FreeMarker、JSP、Velocity、JSON等以及自定义视图渲染)
- public class BlogController extends Controller{
- BlogService service;
- public void index(){
- set("blogPage", service.paginate(getParaToInt(0, 1), 10));
- render("blog.html");
- }
- public void add() {}
- public void save() {
- getModel(Blog.class).save();
- redirect("/blog");
- }
- public void edit()
- {set("blog", service.findById(getParaToInt()));}
- public void update() {
- getModel(Blog.class).update();
- redirect("/blog");
- }
- public void delete() {
- service.deleteById(getParaToInt());
- redirect("/blog");
- }
- }
Service(所有业务与sql放在Service层)
- public class BlogService {
- private Blog dao = new Blog().dao();
- public Page<Blog> paginate(int pageNumber, int pageSize) {
- return dao.paginate(pageNumber, pageSize, "select *", "from blog order by id asc");
- }
- public Blog findById(int id) {return dao.findById(id}
- public void deleteById(int id) {dao.deleteById(id);}
- }
Model(无xml、无annotation、无attribute)
public class Blog extends Model<Blog> {}
Validator(API引导式校验,比xml校验方便N倍,有代码检查不易出错)
- public class BlogValidator extends Validator {
- protected void validate(Controller controller) {
- validateRequiredString("blog.title", "titleMsg", "请输入Blog标题!");
- validateRequiredString("blog.content", "contentMsg", "请输入Blog内容!");
- }
- protected void handleError(Controller controller) {
- controller.keepModel(Blog.class);
- }
- }
拦截器
- public class BlogInterceptor implements Interceptor {
- public void intercept(Invocation inv) {
- System.out.println("Before invoking " + inv.getActionKey());
- inv.invoke();
- System.out.println("After invoking " + inv.getActionKey());
- }
- }