SpringMVC使用restful实现员工修改
1、在数据列表列加上编辑地址:
2、在控制层实现编辑后数据回显@RequestMapping(value="/emp/{id}", method=RequestMethod.GET) public String input(@PathVariable("id") Integer id, Map<String, Object> map){ map.put("employee", employeeDao.get(id)); map.put("departments", departmentDao.getDepartments()); return "input"; }
3、在列表页面点击编辑按钮,查看数据是否可以回显
4、当表单是修改的时候,lastName字段是不能修改的:<c:if test="${employee.id == null }"> <!-- path 属性对应 html 表单标签的 name 属性值 --> LastName: <form:input path="lastName"/> </c:if> <c:if test="${employee.id != null }"> <form:hidden path="id"/> <input type="hidden" name="_method" value="PUT"/> <%-- 对于 _method 不能使用 form:hidden 标签, 因为 modelAttribute 对应的 bean 中没有 _method 这个属性 --%> <%-- <form:hidden path="_method" value="PUT"/> --%> </c:if>
5、实现修改操作的控制层代码。@ModelAttribute public void getEmployee(@RequestParam(va造婷用痃lue="id",required=false) Integer id, Map<String, Object> map){ if(id != null){ map.put("employee", employeeDao.get(id)); } } @RequestMapping(value="/emp", method=RequestMethod.PUT) public String update(Employee employee){ employeeDao.save(employee); return "redirect:/emps"; }
6、修改列表中的一条记录查看是否能够修改成功。数据能够修改,并且跳转到了数据列表页面。