springboot里面的更新实现以及update.jsp页面以及为什么更新要先通过id获得整个对象再去update。
为什么更新要先通过id获得整个对象再去update?
其实也是可以直接通过id获得整个对象的,但是我们这里要得到整个对象是因为
1、想把整个对象的数据显示再jsp页面上。要不只能知道id,别的不知道都。
2、最重要的是你update的是整个对象,你不得到整个对象,你传值传不过去,只能传到id别的没有。
首先:更新的话是有俩个步骤,通过id获得整个对象,然后再通过id更新。
1、控制层
@PostMapping("update") public String update(Emp emp){ empService.update(emp); return "redirect:/emp/findAll"; } @GetMapping("findOne") public String findOne(String id,Model model){ System.out.println("id = " + id); Emp emp=empService.findById(id); System.out.println("emp = " + emp); model.addAttribute("emp",emp); return "/ems/updateEmp"; }
2、业务层
public interface EmpService { List<Emp> findAll(); void save(Emp emp); void delete(String id); Emp findById(String id); void update(Emp emp); }
@Override public void update(Emp emp) { empDAO.update(emp); } @Override public Emp findById(String id) { return empDAO.findById(id); }
3、dao层
@Repository public interface EmpDAO { public List<Emp> findAll() ; // void save(Emp emp); void delete(String id); Emp findById(String id); void update(Emp emp); }
4、.xml文件
<select id="findById" parameterType="String" resultType="Emp"> select id,name,salary,age from t_emp where id=#{id} </select> <update id="update" parameterType="Emp"> update t_emp set name=#{name},salary=#{salary},age=#{age} where id=#{id} </update>
update.jsp
<%@page isELIgnored="false" contentType="text/html; 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> <title>update Emp</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/ems/css/style.css" /> </head> <body> <div id="wrap"> <div id="top_content"> <div id="header"> <div id="rightheader"> <p> 2009/11/20 <br /> </p> </div> <div id="topheader"> <h1 id="title"> <a href="#">Main</a> </h1> </div> <div id="navigation"> </div> </div> <div id="content"> <p id="whereami"> </p> <h1> update Emp info: </h1> <form action="${pageContext.request.contextPath}/emp/update" method="post"> <table cellpadding="0" cellspacing="0" border="0" class="form_table"> <tr> <td valign="middle" align="right"> id: </td> <td valign="middle" align="left"> ${requestScope.emp.id} <input type="hidden" name="id" value="${requestScope.emp.id}"> </td> </tr> <tr> <td valign="middle" align="right"> name: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="name" value="${requestScope.emp.name}"/> </td> </tr> <tr> <td valign="middle" align="right"> salary: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="salary" value="${requestScope.emp.salary}"/> </td> </tr> <tr> <td valign="middle" align="right"> age: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="age" value="${requestScope.emp.age}"/> </td> </tr> </table> <p> <input type="submit" class="button" value="Confirm" /> </p> </form> </div> </div> <div id="footer"> <div id="footer_bg"> ABC@126.com </div> </div> </div> </body> </html>
报错的一个点:
emplist.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>emplist</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/ems/css/style.css" /> </head> <body> <div id="wrap"> <div id="top_content"> <div id="header"> <div id="rightheader"> <p> 2009/11/20 <br /> </p> </div> <div id="topheader"> <h1 id="title"> <a href="#">main</a> </h1> </div> <div id="navigation"> </div> </div> <div id="content"> <p id="whereami"> </p> <h1> Welcome! </h1> <table class="table"> <tr class="table_header"> <td> ID </td> <td> Name </td> <td> Salary </td> <td> Age </td> <td> Operation </td> </tr> <c:forEach items="${requestScope.emps}" var="emp"> <tr class="row1"> <td> ${emp.id} </td> <td> ${emp.name} </td> <td> ${emp.salary} </td> <td> ${emp.age} </td> <td> <a href="${pageContext.request.contextPath}/emp/delete?id=${emp.id}">delete emp</a> <a href="${pageContext.request.contextPath}/emp/findOne?id=${emp.id}">update emp</a> </td> </tr> </c:forEach> </table> <p> <input type="button" class="button" value="Add Employee" onclick="location='${pageContext.request.contextPath}/ems/addEmp.jsp'"/> </p> </div> </div> <div id="footer"> <div id="footer_bg"> ABC@126.com </div> </div> </div> </body> </html>
<a href="${pageContext.request.contextPath}/emp/findOne?id=${emp.id}">update emp</a>
就是因为这个
findOne?id=${emp.id}中的id没写导致传不过去值。
along