Spring MVC - 如何使用JSP表更新数据库中的行?

时间:2016-02-18 10:08:59

标签: java mysql spring jsp spring-mvc

我在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>

在这种情况下,输入的值不为空(我用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;

}

1 个答案:

答案 0 :(得分:0)

假设这是您项目中的第一个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应用程序。