一:提交一个带上id的修改请求
<td><a href="emp/${emp.empId }">修改</a> </td>
二:在controller中捕捉该请求
/*
* value="emp/{id} {id}是请求中带入的参数
* method=RequestMethod.GET 捕获get请求的方法
* @PathVariable("id") Integer id 捕获请求中的id
* Map<String, Object> map map是request中的一个map,里面可存值并且在转发页面回显
*/
@RequestMapping(value="emp/{id}", method=RequestMethod.GET)
public String updateUI(@PathVariable("id") Integer id, Map<String, Object> map){
map.put("employee", employeeServce.get(id));
map.put("departments", departmentService.getAll());
return "add";
}
三:在页面进行回显操作
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<!--
form是springMVC中自带的一种可以是表单回显方便的表达式
modelAttrbute 必须要和map中放的实体名一致
-->
<form:form action="emp" method="POST" modelAttribute="employee">
<!-- path 表示name -->
<!-- id为空则是修改方法 -->
<c:if test="${employee.empId == null }">
name:<form:input path="empName"/><br>
</c:if>
<c:if test="${employee.empId != null }">
<form:hidden path="empId"/>
<!-- 将提交方法从post转换成PUT方法,此操作需要在web.xml中配置一些东西 -->
<input type="hidden" name="_method" value="PUT" />
</c:if>
email:<form:input path="email"/><br>
<%
Map<String, String> genders = new HashMap<String, String>();
genders.put("M", "男");
genders.put("F", "女");
request.setAttribute("genders", genders);
%>
gender:<form:radiobuttons path="gender" items="${genders}"/> <br>
department:<form:select path="dId" items="${departments }" itemLabel="deptName" itemValue="deptId"></form:select>
<br>
<input type="submit" value="submit"/>
</form:form>
</body>
</html>
将要修改的数据提交请求
//先行缓冲原来的employee,
/*
* @ModelAttribute 可以在springmvc中保存数据,并可以在下面的的请求中提取出来
*/
@ModelAttribute
public void getEmployee(@RequestParam(value="empId",required=false) Integer empId,
Map<String, Object> map){
if(empId != null){
map.put("employee", employeeServce.get(empId));
}
}
//在缓存的employee的基本上来修改employee
@RequestMapping(value="/emp", method=RequestMethod.PUT)
public String update(Employee employee){
System.out.println("aaaa");
employeeServce.save(employee);
return "redirect:/emps";
}
完成修改