4.使用JSP展示后台取到的数据(增删改查)

来源:互联网 发布:波段轻松涨停指标源码 编辑:程序博客网 时间:2024/06/05 15:12

接上一篇的工程
(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号:
这里写图片描述
提交后到展示页面:
这里写图片描述

原创粉丝点击