每日总结

今天学习了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");
    }
}
复制代码

页面的布局和样例中一样。

 

posted @   一个小虎牙  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示