/**
* 如果直接發送ajax=PUT形式的請求
* 封裝的數據
* Employee
* [empId=1014, empName=null, gender=null, email=null, dId=null]
*
* 問題:
* 請求體中有數據;
* 但是Employee對象封裝不上;
* update tbl_emp where emp_id = 1014;
*
* 原因:
* Tomcat:
* 1、將請求體中的數據,封裝一個map。
* 2、request.getParameter("empName")就會從這個map中取值。
* 3、SpringMVC封裝POJO對象的時候。
* 會把POJO中每個屬性的值,request.getParamter("email");
* AJAX發送PUT請求引發的血案:
* PUT請求,請求體中的數據,request.getParameter("empName")拿不到
* Tomcat一看是PUT不會封裝請求體中的數據為map,只有POST形式的請求才封裝請求體為map
* org.apache.catalina.connector.Request--parseParameters() (3111);
*
* protected String parseBodyMethods = "POST";
* if( !getConnector().isParseBodyMethod(getMethod()) ) {
success = true;
return;
}
*
*
* 解決方案;
* 我們要能支持直接發送PUT之類的請求還要封裝請求體中的數據
* 1、配置上HttpPutFormContentFilter;
* 2、他的作用;將請求體中的數據解析包裝成一個map。
* 3、request被重新包裝,request.getParameter()被重寫,就會從自己封裝的map中取數據
* 員工更新方法
* @param employee
* @return
*/
@ResponseBody
@RequestMapping(value="/emp/{empId}",method=RequestMethod.PUT)
public Msg saveEmp(Employee employee,HttpServletRequest request){
System.out.println("請求體中的值:"+request.getParameter("gender"));
System.out.println("將要更新的員工數據:"+employee);
employeeService.updateEmp(employee);
return Msg.success() ;
}
在參數中加了Employee,request會自動把請求中的數據封裝到employee中