JFinal 是 JAVA 极速 WEB 开发的开源框架
一、环境:MyEclipse9+Tomcat6+MySql;
二、准备 jar 包,如下图:
三、开始在MyEclipse9+Tomcat6+MySql部署一个学生信息管理小Demo:
- 创建Web Project工程;
- 在WEB-INFO文件目录下新建lib文件夹并放入四个jar包,然后build path;
创建jfinalstudy01数据库并创建两张表student+classes,如下:
1234567891011121314create table classes(classesid int(4) not null,classesname varchar(40),classesaddress varchar(60));create table student(studentid int(4) not null,studentname varchar(10),studentage int(4),studentsex varchar(4),classesid int(4));添加以下配置至web.xml(同时新建JFinalAllConfig类):
123456789101112131415161718192021222324252627282930313233343536373839public class JFinalAllConfig extends JFinalConfig {public void configConstant(Constants me) {me.setDevMode(true);}public void configRoute(Routes me) {me.add("/", StudentController.class);me.add("/student", StudentController.class);}public void configPlugin(Plugins me) {C3p0Plugin cp = new C3p0Plugin("jdbc:mysql://localhost:3306/JFinalStudy01", "root", "");me.add(cp);ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);me.add(arp);// 三个参数依次为表名、主键、modelarp.addMapping("student", "studentid", Student.class);arp.addMapping("classes", "classesid", Classes.class);}public void configInterceptor(Interceptors me) {// TODO Auto-generated method stub}public void configHandler(Handlers me) {// TODO Auto-generated method stub}}新建Student、Classes的model:
Student:
1234567891011121314package com.phy.jfinal.model;import com.jfinal.plugin.activerecord.Model;public class Student extends Model {private static final long serialVersionUID = 1L;public static final Student dao = new Student();public Classes getClasses() {return Classes.dao.findById(get("classesid"));}}Classes:
12345678910package com.phy.jfinal.model;import com.jfinal.plugin.activerecord.Model;public class Classes extends Model {private static final long serialVersionUID = 1L;public static final Classes dao = new Classes();}
新建StudentController(接收HTTP请求处理的控制器):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051package com.phy.jfinal.controller;import java.util.List;import com.jfinal.aop.Before;import com.jfinal.core.Controller;import com.phy.jfinal.interceptor.StudentInterceptor;import com.phy.jfinal.model.Student;import com.phy.jfinal.validator.StudentValidator;public class StudentController extends Controller {private static int num = 0;// 进行获取学生信息列表前的拦截操作public void index() {List list = Student.dao.find("select * from student");setAttr("studentList", list);render("/index.html");}public void add() {render("/add.html");}public void delete() {// 获取url请求中第一个值Student.dao.deleteById(getParaToInt());forwardAction("/student");}public void update() {Student student = getModel(Student.class);student.update();forwardAction("/student");}public void get() {Student student = Student.dao.findById(getParaToInt());setAttr("student", student);render("/index2.html");}//StudentValidator是对进行保存操作时的变量的验证public void save() {Student student = getModel(Student.class);student.set("studentid", num++).save();forwardAction("/student");}}拦截器及验证器:
拦截器
1234567891011121314package com.phy.jfinal.interceptor;import com.jfinal.aop.Interceptor;import com.jfinal.core.ActionInvocation;public class StudentInterceptor implements Interceptor {public void intercept(ActionInvocation ai) {System.out.println("action注入之前");ai.invoke();System.out.println("action注入之后");}}验证器
123456789101112131415161718192021package com.phy.jfinal.validator;import com.jfinal.core.Controller;import com.jfinal.validate.Validator;public class StudentValidator extends Validator {//在校验失败时才会调用protected void handleError(Controller controller) {controller.keepPara("student.studentname");//将提交的值再传回页面以便保持原先输入的值controller.render("/add.html");}protected void validate(Controller controller) {//验证表单域name,返回信息key,返回信息valuevalidateRequiredString("student.studentname", "studentnameMsg","请输入学生名称!");}}
前台实现(通过freemarker读取数据),在WebRoot目录下新建index.html,add.html,change.html(分别为学生信息列表获取、学生信息添加、学生信息修改,下面依次列出实现代码):
- index.html
- add.html
- change.html
- index.html
三个CSS文件(添加在WebRoot目录下的css文件夹下:
- common.css
- add.css
- index.css
- common.css
以下分别为整体项目目录结构及相关截图: