今天学习了JFinal web framework的开发。
编写了一个简单的crud的程序。
package com.demo.student; import com.demo.common.model.Student; import com.jfinal.aop.Before; import com.jfinal.aop.Inject; import com.jfinal.core.Controller; import com.jfinal.core.Path; @Path("/stu") public class StudentController extends Controller { @Inject StudentService service; public void index() { setAttr("stuPage", service.paginate(getParaToInt(0, 1), 10)); render("stu.html"); } public void select() { setAttr("stuPage", service.select(getParaToInt(0, 1), 10, getPara("name"))); render("stu.html"); } public void add() { } /** * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中, * 并要对数据进正确性进行验证,在此仅为了偷懒 */ @Before(StudentValidator.class) public void save() { getBean(Student.class).save(); redirect("/stu"); } public void edit() { setAttr("student", service.findById(getParaToInt())); } /** * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中, * 并要对数据进正确性进行验证,在此仅为了偷懒 */ @Before(StudentValidator.class) public void update() { getBean(Student.class).update(); redirect("/stu"); } public void delete() { service.deleteById(getParaToInt()); redirect("/stu"); } }
package com.demo.student; import com.demo.common.model.Student; import com.jfinal.plugin.activerecord.Page; public class StudentService { private Student dao = new Student().dao(); public Page<Student> paginate(int pageNumber, int pageSize) { return dao.paginate(pageNumber, pageSize, "select *", "from student order by id asc"); } public Page<Student> select(int pageNumber, int pageSize,String value1) { System.out.println(pageNumber+pageSize+value1); return dao.paginate(pageNumber, pageSize, "select *", "from student where name = ? order by id asc", value1); } public Student findById(int id) { return dao.findById(id); } public void deleteById(int id) { dao.deleteById(id); } }
package com.demo.student; import com.demo.common.model.Student; import com.jfinal.core.Controller; import com.jfinal.validate.Validator; public class StudentValidator extends Validator { protected void validate(Controller controller) { validateRequiredString("student.stuid", "stuidMsg", "请输入学生学号!"); validateRequiredString("student.name", "nameMsg", "请输入学生姓名!"); validateRequiredString("student.age", "ageMsg", "请输入学生年龄!"); } protected void handleError(Controller controller) { controller.keepModel(Student.class); String actionKey = getActionKey(); if (actionKey.equals("/stu/save")) controller.render("add.html"); else if (actionKey.equals("/stu/update")) controller.render("edit.html"); } }
页面的布局和样例中一样。
标签:总结,student,每日,void,Student,import,com,public From: https://www.cnblogs.com/syhxx/p/17877199.html