簡體   English   中英

Spring MVC - 如何使用JSP表更新數據庫中的行?

[英]Spring MVC - how to update row in database using JSP table?

我在jsp表中遇到更新輸入值的問題,我不知道我應該在哪里放置form actionc:forEach以便一切都可以使用。 問題出在哪兒?

在這種情況下,輸入的值接收null ,單擊“編輯”后,每行都為空:

<div class="forms">
    <div class="selectr">
    <p style="font-size: 13"> 
    <table border="1">
        <tr style="font-size: 13">
            <td>ID</td>
            <td>First name</td>
            <td>Last name</td>
            <td>Gender</td>
            <td>Position</td>
            <td>Salary</td>
            <td>Phone number</td>
            <td>Address</td>
            <td>Action</td>
        </tr>
        <c:forEach items="${employees}" var="employee">
            <tr style="font-size: 10">
                <td><input disabled style="width: 17px" type="text" name="id" value="${employee.id}"></td>
                <td><input style="width: 75px" type="text" name="name" value="${employee.name}"></td>
                <td><input style="width: 75px" type="text" name="lastName" value="${employee.lastName}"></td>
                <td><input style="width: 60px"  type="text" name="gender" value="${employee.gender}"></td>
                <td><input style="width: 80px" type="text" name="position" value="${employee.position}"></td>
                <td><input style="width: 60px" type="text" name="salary" value="${employee.salary}"></td>
                <td><input style="width: 100px" type="text" name="phoneNumber" value="${employee.phoneNumber}"></td>
                <td><input style="width: 160px" type="text" name="address" value="${employee.address}"></td>
                <td><form action="/VirtualClinic/employeelist.html?editEmployee=${employee.id}"  method="POST"><input type="submit" value="Edit"/></td>
            </tr>
            </c:forEach>
    </table>
    </div>

在這種情況下,輸入的值不為null(我使用System.out.println(employee.getName()檢查它,但他們仍然不想更新,因為點擊更新后的值從數據庫恢復到默認值):

 <form action="/VirtualClinic/employeelist.html?editEmployee="   method="POST">
 <div class="forms">
    <div class="selectr">
    <p style="font-size: 13"> 
    <table border="1">
        <tr style="font-size: 13">
            <td>ID</td>
            <td>First name</td>
            <td>Last name</td>
            <td>Gender</td>
            <td>Position</td>
            <td>Salary</td>
            <td>Phone number</td>
            <td>Address</td>
            <td>Action</td>
        </tr>
        <c:forEach items="${employees}" var="employee">
            <tr style="font-size: 10">
                <td><input disabled style="width: 17px" type="text" name="id" value="${employee.id}"></td>
                <td><input style="width: 75px" type="text" name="name" value="${employee.name}"></td>
                <td><input style="width: 75px" type="text" name="lastName" value="${employee.lastName}"></td>
                <td><input style="width: 60px"  type="text" name="gender" value="${employee.gender}"></td>
                <td><input style="width: 80px" type="text" name="position" value="${employee.position}"></td>
                <td><input style="width: 60px" type="text" name="salary" value="${employee.salary}"></td>
                <td><input style="width: 100px" type="text" name="phoneNumber" value="${employee.phoneNumber}"></td>
                <td><input style="width: 160px" type="text" name="address" value="${employee.address}"></td>
                <td><input type="submit" value="Edit"/></td>
            </tr>
            </c:forEach>
    </table>
    </div>
</div>
</form>

DAO:

public void updateEmployee(Employee employee) {
    String query = "update virtualclinic.employee SET name=?, lastname=?, gender=?,"
            + "position=?, salary=?, phonenumber=?, address=? WHERE idemployee=?";
    Connection con = null;
    PreparedStatement ps = null;
    try{
        con = dataSource.getConnection();
        ps = con.prepareStatement(query);
        ps.setString(1, employee.getName());
        ps.setString(2, employee.getLastName());
        ps.setString(3, employee.getGender());
        ps.setString(4, employee.getPosition());
        ps.setString(5, employee.getSalary());
        ps.setString(6, employee.getPhoneNumber());
        ps.setString(7, employee.getAddress());
        ps.setString(8, employee.getId());

        int out = ps.executeUpdate();                    

    }catch(SQLException e){
        e.printStackTrace();
    }finally{
        try {
            ps.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

服務:

public void updateEmployee(Employee employee) {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("clinicconfig.xml");
    employeeDAO = ctx.getBean("employeeDAO", EmployeeDAOImpl.class);            

    employeeDAO.updateEmployee(employee);

}

控制器:

    @RequestMapping(value = "/employeelist.html", method = RequestMethod.POST)
public ModelAndView deleteEmployee(Model model, @ModelAttribute("employee") Employee employee, @RequestParam String editEmployee) throws SQLException {

    setAppContext();

    clinicService.updateEmployee(employee);

    System.out.println(employee.getName());

    List<Employee> employees = clinicService.getAllEmployees();
    model.addAttribute("employees", employees);

    ModelAndView mstaff = new ModelAndView("EmployeeList");
    return mstaff;

}

假設這是你項目中的第一個JSP。 此外,我沒有看到您的tld導入,但是EL和JSTL問題最常見的原因之一是您使用的JSP版本,您使用的JSTL版本以及您的方式之間的配置不匹配在部署描述符(web.xml)中聲明了您的Web應用程序。

對於JSP 2.1容器(例如Tomcat 6),您應該使用JSTL 1.2,並且應該使用Servlets 2.5 XML Schema將Web應用程序聲明為Servlets 2.5 Web應用程序。

對於JSP 2.0容器(例如Tomcat 5),您應該使用JSTL 1.1,並且應該使用Servlets 2.4 XML Schema將Web應用程序聲明為Servlets 2.4 Web應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM