JFinal is an ultra-fast WEB + ORM framework based on the Java language. Its core design goals are rapid development, minimal code, simple learning, powerful functionality, lightweight, easy to expand, and Restful. While possessing all the advantages of the Java language, it also has the development efficiency of dynamic languages like ruby and python! Save more time for you to spend with your loved ones, family, and friends ;)
Join JFinal's ultra-fast WeChat official account development: JFinal
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>5.1.1</version>
</dependency>
1. Controller (Supports Enjoy, JSP, JSON, etc., as well as custom view rendering)
@Before(BlogInterceptor.class)
public class BlogController extends Controller {
@Inject
BlogService service;
public void index() {
set("blogPage", service.paginate(getInt(0, 1), 10));
render("blog.html");
}
public void add() {
}
@Before(BlogValidator.class)
public void save() {
getModel(Blog.class).save();
redirect("/blog");
}
public void edit() {
set("blog", service.findById(getInt()));
}
@Before(BlogValidator.class)
public void update() {
getModel(Blog.class).update();
redirect("/blog");
}
public void delete() {
service.deleteById(getInt());
redirect("/blog");
}
}
2. All business and SQL are placed in the Service layer
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);
}
}
3. Model (No XML, no annotations, no attributes)
public class Blog extends Model<Blog> {
}
4. Validator (API-guided validation, much more convenient than XML validation, code-checked to minimize errors)
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);
}
}
5. Interceptor (Only for demonstration in this demo, this demo does not require this interceptor)
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());
}
}