以下是JFinal实现Blog管理的示例:
1:控制器(支持FreeMarker、JSP、Velocity、JSON等等以及自定义视图渲染)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@Before(BlogInterceptor.class)
public class BlogController extends Controller {
public void index() {
setAttr("blogList", Blog.me.find("select * from blog"));
}
public void add() {
}
@Before(BlogValidator.class)
public void save() {
getModel(Blog.class).save();
}
public void edit() {
setAttr("blog", Blog.me.findById(getParaToInt()));
}
@Before(BlogValidator.class)
public void update() {
getModel(Blog.class).update();
}
public void delete() {
Blog.me.deleteById(getParaToInt());
}
}
|
2:Model(无xml、无annotation、无attribute、无getter、无setter、new Blog()这行代码也不是必须)
1
2
3
|
public class Blog extends Model {
public static final Blog me = new Blog();
}
|
3:Validator(API引导式校验,比xml校验方便N倍,有代码检查不易出错)
1
2
3
4
5
6
7
8
9
10
|
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不需要此拦截器)
1
2
3
4
5
6
7
|
public class BlogInterceptor implements Interceptor {
public void intercept(ActionInvocation ai) {
System.out.println("Before invoking " + ai.getActionKey());
ai.invoke();
System.out.println("After invoking " + ai.getActionKey());
}
} 文章转自e良师益友网学习社区 |