源码下载 :
http://download.csdn.net/download/knight_black_bob/9160015
sql
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `clazz`
-- ----------------------------
DROP TABLE IF EXISTS `clazz`;
CREATE TABLE `clazz` (
`clazzid` int(11) NOT NULL AUTO_INCREMENT,
`clazzname` varchar(255) DEFAULT NULL,
`clazzaddr` varchar(255) DEFAULT NULL,
PRIMARY KEY (`clazzid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of clazz
-- ----------------------------
INSERT INTO `clazz` VALUES ('1', 'java', '信息学院');
INSERT INTO `clazz` VALUES ('2', 'php', '信息学院');
-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`stuid` int(11) NOT NULL AUTO_INCREMENT,
`stuname` varchar(255) DEFAULT NULL,
`stuage` int(11) DEFAULT NULL,
`stusex` int(11) DEFAULT NULL,
`clazzid` int(11) DEFAULT NULL,
PRIMARY KEY (`stuid`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('2', '2', '2', '2', '1');
INSERT INTO `student` VALUES ('9', '5', '5', '5', '1');
INSERT INTO `student` VALUES ('11', '2', '2', '2', '1');
INSERT INTO `student` VALUES ('12', '5', '5', '5', '1');
pom.xml
手动添加 jfinal-1.8.jar 文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.baoy.cn
Demo1
0.0.1-SNAPSHOT
Demo1 Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
c3p0
c3p0
0.9.1.2
compile
org.freemarker
freemarker
2.3.23
compile
com.jfinal
jetty-server
8.1.8
compile
mysql
mysql-connector-java
5.1.17
compile
Demo1
org.eclipse.m2e
lifecycle-mapping
1.0.0
org.apache.maven.plugins
maven-dependency-plugin
[2.0,)
copy-dependencies
unpack
org.apache.maven.plugins
maven-dependency-plugin
2.8
prepare-package
copy-dependencies
src/main/webapp/WEB-INF/lib
web.xml
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
jfinal
com.jfinal.core.JFinalFilter
configClass
baoyou.config.DemoConfig
jfinal
/*
DemoConfig.java
package baoyou.config;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import baoyou.controller.ClazzController;
import baoyou.controller.StudentController;
import baoyou.model.Clazz;
import baoyou.model.Student;
public class DemoConfig extends JFinalConfig {
@Override
public void configConstant(Constants me) {
}
@Override
public void configHandler(Handlers arg0) {
// TODO Auto-generated method stub
}
@Override
public void configInterceptor(Interceptors arg0) {
// TODO Auto-generated method stub
}
@Override
public void configPlugin(Plugins me) {
C3p0Plugin cp = new C3p0Plugin("jdbc:mysql://localhost:3306/demo", "root", "root");
me.add(cp);
ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
me.add(arp);
arp.addMapping("student", "stuid", Student.class);
arp.addMapping("clazz", "clazzid", Clazz.class);
}
@Override
public void configRoute(Routes me) {
me.add("/", StudentController.class);
me.add("/student", StudentController.class);
me.add("/clazz", ClazzController.class);
}
}
package baoyou.model;
import com.jfinal.plugin.activerecord.Model;
public class Student extends Model {
public static final Student dao = new Student();
public Clazz getClazz() {
return Clazz.dao.findById(get("clazzid"));
}
}
package baoyou.model;
import com.jfinal.plugin.activerecord.Model;
public class Clazz extends Model{
public static final Clazz dao = new Clazz();
}
package baoyou.intercepter;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
public class StudentInterceptor implements Interceptor {
@Override
public void intercept(ActionInvocation ai) {
System.out.println("Before action invoking");
ai.invoke();
System.out.println("After action invoking");
}
}
package baoyou.validator;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;
public class StudentValidator extends Validator {
//在校验失败时才会调用
@Override
protected void handleError(Controller controller) {
controller.keepPara("student.stuname");//将提交的值再传回页面以便保持原先输入的值
controller.render("/add.html");
}
@Override
protected void validate(Controller controller) {
//验证表单域name,返回信息key,返回信息value
validateRequiredString("student.stuname", "stunameMsg",
"请输入学生名称!");
}
}
package baoyou.controller;
import java.util.List;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import baoyou.intercepter.StudentInterceptor;
import baoyou.model.Student;
import baoyou.validator.StudentValidator;
public class StudentController extends Controller {
@Before(StudentInterceptor.class)
public void index() {
List list = Student.dao.find("select * from student");
setAttr("studentList", list);
render("/list.html");
}
public void add() {
System.out.println("------------");
render("/add.html");
}
public void delete() {
// 获取表单域名为studentID的值
// Student.dao.deleteById(getPara("studentID"));
// 获取url请求中第一个值
Student.dao.deleteById(getParaToInt());
// forwardAction("/student");
redirect("/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");
}
public void save() {
Student student = getModel(Student.class);
student.save();
redirect("/student");
}
}
姓名 | 年龄 | 性别 | 班级 | 操作 |
${student.stuname} | ${student.stuage} | ${student.stusex} | ${student.getClazz().clazzname} |
#list>
姓名:
${stunameMsg!}
年龄:
性别:
班级:
捐助开发者
在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!