接上一篇的工程
(1)展示所有数据
修改HelloController:
从数据库获取所有记录,并设置参数以便后台可以取到。
修改hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/hello/save">添加</a>
<table>
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
<td>password</td>
<td>操作</td>
</tr>
<c:forEach items="${users}" var="l">
<tr>
<td>${l.id}</td>
<td>${l.name}</td>
<td>${l.age}</td>
<td>${l.password}</td>
<td>
<a href="${pageContext.request.contextPath}/hello/delete/${l.id}">删除</a>
<a href="${pageContext.request.contextPath}/hello/update/${l.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
访问http://localhost:8080/jfinal_demo02/hello/hello
(2)加入添加数据功能
在HelloController中新增一个方法:
(1) public void save(){
(2) User user= getModel(User.class,”“); //获取前端传来的数据模型
(3) String method=getRequest().getMethod();
(4) if(method.equalsIgnoreCase(“get”)){ //如果是get请求就到新增页面
(5) render(“save.jsp”);
(6) }else{
(7) if(user.get(“id”)!=null){
(8) System.out.println(user);
(9) user.save(); //如果是POST请求,插入获取到的表单数据
(10) }
(11) redirect(“/hello/hello”);
(12) }
(13)
(14) }
在hello.jsp加入新增链接,对应HelloController中的save:
新建一个save.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="" method="post">
id:<input type="text" name="id" />
name:<input type="text" name="name" />
age:<input type="text" name="age" />
password:<input type="text" name="password" />
<input type="submit" value="submit" />
</form>
</body>
</html>
访问http://localhost:8080/jfinal_demo02/hello/hello
点击添加按钮,此为get请求,到对应的save.jsp页面:
点击提交,此为post请求,后台新增数据后到hello.jsp
(3)添加删除功能和修改功能
修改hello.jsp,注意链接后面会传入id参数,后台获取此参数进行修改和更新
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/hello/save">添加</a>
<table>
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
<td>password</td>
<td>操作</td>
</tr>
<c:forEach items="${users}" var="l">
<tr>
<td>${l.id}</td>
<td>${l.name}</td>
<td>${l.age}</td>
<td>${l.password}</td>
<td>
<a href="${pageContext.request.contextPath}/hello/delete/${l.id}">删除</a>
<a href="${pageContext.request.contextPath}/hello/update/${l.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
在HelloController中添加更新和删除方法:
public void delete(){
int id=getParaToInt();
User.dao.deleteById(id);
redirect("/hello/hello");
}
public void update(){
Integer id = getParaToInt();
String method = getRequest().getMethod();
if (method.equalsIgnoreCase("get")) {
setAttr("id", id);
User user = User.dao.findById(id);
setAttr("users", user);
render("update.jsp");
} else if (method.equalsIgnoreCase("post")) {
User user= getModel(User.class,"");
user.update();
redirect("/hello/hello");
}
}
新增一个update.jsp
Update.jsp内容如下:
访问:http://localhost:8080/jfinal_demo02/hello/hello
点击3号删除:
修改2号:
提交后到展示页面: