【问题标题】:Spring MVC - how to update row in database using JSP table?Spring MVC - 如何使用 JSP 表更新数据库中的行?
【发布时间】:2016-05-30 10:34:12
【问题描述】:

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

道:

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;

}

【问题讨论】:

  • 最好使用 Ajax 而不是表单操作来单独更新每一行。否则你必须改变你的 html 设计,因为表单动作写在表格的末尾,但表单元素应该在表单标签内。
  • 使用 ajax 更新行或创建在另一个页面上显示现有数据的编辑按钮,然后使用单个表单更新数据

标签: java mysql spring jsp spring-mvc


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2017-10-15
    • 1970-01-01
    • 2016-04-09
    • 2015-06-15
    • 1970-01-01
    相关资源
    最近更新 更多