使用SpringMVC出現The request sent by the client was syntactically incorrect.請求錯誤如下:
可以確定爲提交的表單數據和目標方法的入參不一致所導致,表單數據可以多於目標入參個數,但目標參數沒有被賦值,則會出現該異常,如下情況:
表單數據:
目標方法:
Employee字段:
比對錶單數據和Employee字段,可以發現,表單數據比Employee字段少,再加上目標方法的入參是一個
Employee對象,所以將會出現提交的數據不足,解決的辦法:
①直接使用@RequestParam()標籤,指定和表單數據相同的入參然後再裝配Employee對象
②從數據庫中加載一個Employee對象,讓表單提交的數據在加載的對象上進行修改,沒有修改的字段將會原封不動:
@ModelAttribute
public void getEmployee(@RequestParam(value="id", required=false)
Integer id,Map<String, Object> map)
{
Employee employee;
if(id != null)
{
employee = employeeService.getEmpById(id);
map.put("employee", employee);
}
}
在加載對象時注意:Employee的所有字段都要查詢出來,所以,對於底層的DAO就不能使用懶加載,如果使用了懶加載也將會出現該錯誤。