1. 程式人生 > 實用技巧 >springboot裡面的更新實現以及update.jsp頁面

springboot裡面的更新實現以及update.jsp頁面

首先:更新的話是有倆個步驟,通過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>&nbsp;
                                    <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沒寫導致傳不過去值。