?
源码下载 :
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 ?文件
<project xmlns="http://maven.apache.org/POM/4.0.0" 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"> <modelVersion>4.0.0</modelVersion> <groupId>com.baoy.cn</groupId> <artifactId>Demo1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Demo1 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>jetty-server</artifactId> <version>8.1.8</version> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.17</version> <scope>compile</scope> </dependency> </dependencies> <build> <finalName>Demo1</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <versionRange>[2.0,)</versionRange> <goals> <goal>copy-dependencies</goal> <goal>unpack</goal> </goals> </pluginExecutionFilter> <action> <ignore /> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>src/main/webapp/WEB-INF/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
?
?
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <filter> <filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class> <init-param> <param-name>configClass</param-name> <param-value>baoyou.config.DemoConfig</param-value> </init-param> </filter> <filter-mapping> <filter-name>jfinal</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
?
?
?
?
?
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<Student> {
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<Clazz>{
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<Student> 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");
}
}
?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link href="/css/manage.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/js/jquery-1.4.4.min.js" type="text/javascript" ></script>
<#-- base href="${CPATH}" / -->
</head>
<body>
<a href="./student/add">添加</a>
<table border="1">
<tr>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
性别
</td>
<td>
班级
</td>
<td>
操作
</td>
</tr>
<#list studentList as student>
<tr>
<td>
${student.stuname}
</td>
<td>
${student.stuage}
</td>
<td>
${student.stusex}
</td>
<td>
${student.getClazz().clazzname}
</td>
<td>
<a href="./student/delete/${student.stuid}">删除</a>
<a href="./student/get/${student.stuid}">修改</a>
</td>
</tr>
</#list>
</table>
</body>
</html>
?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link href="/css/manage.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/js/jquery-1.4.4.min.js" type="text/javascript" ></script>
<#-- base href="${CPATH}" / -->
</head>
<body>
<form action="./save" method="post">
姓名:
<input type="text" name="student.stuname" />${stunameMsg!}
<br />
年龄:
<input type="text" name="student.stuage" />
<br />
性别:
<input type="text" name="student.stusex" />
<br />
班级:
<input type="text" name="student.clazzid" />
<br />
<input type="submit" value="保存" />
</form>
</body>
</html>
?
?
?
?
?
?
?
?
?
(比springmvc更快的开发)jfinal 快速开发入门 freemarker mysql
原文:http://knight-black-bob.iteye.com/blog/2247385