JFinal实现Blog管理的代码

如果需要下载demo项目、源码及详细文档请到: http://www.jfinal.com

1:控制器(支持FreeMarker、JSP、Velocity、JSON等等以及自定义视图渲染)

@Before(BlogInterceptor.class)
public class BlogController extends Controller {
	public void index() {
		setAttr("blogList", Blog.dao.find("select * from blog order by id asc"));
	}
	
	public void add() {
	}
	
	@Before(BlogValidator.class)
	public void save() {
		getModel(Blog.class).save();
	}
	
	public void edit() {
		setAttr("blog", Blog.dao.findById(getParaToInt()));
	}
	
	@Before(BlogValidator.class)
	public void update() {
		getModel(Blog.class).update();
	}
	
	public void delete() {
		Blog.dao.deleteById(getParaToInt());
	}
}

 

2:Model(无xml、无annotaion、无attribute、无getter、无setter,其中new Blog()这行代码也可以删掉不是必须)
public class Blog extends Model<Blog> {
	public static final Blog dao = new Blog();
}

3: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);
	}
}

 4:拦截器(在此demo中仅为示例,本demo不需要此拦截器)

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());
	}
}

 如果需要下载demo项目、源码及详细文档请到: http://www.jfinal.com

你可能感兴趣的:(mvc,framework,ActiveRecord,jFinal)