SpringMVC表單提交參數出現400錯誤的解決方法?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
1.參數指定問題
如果Controller中定義了參數,而表單內卻沒有定義該字段
@SuppressWarnings("deprecation") @RequestMapping("/hello.do") public String hello(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="userName") String user ){ request.setAttribute("user", user); return "hello"; }
這里,表單內必須提供一個userName的屬性!
不想指定的話,你也可以定義這個屬性的默認值defaultValue="":
@SuppressWarnings("deprecation") @RequestMapping("/hello.do") public String hello(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="userName",defaultValue="佚名") String user ){ request.setAttribute("user", user); return "hello"; }
也可以指定該參數是非必須的required=false:
@SuppressWarnings("deprecation") @RequestMapping("/hello.do") public String hello(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="userName",required=false) String user ){ request.setAttribute("user", user); return "hello"; }
2.上傳問題
上傳文件大小超出了Spring上傳的限制
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 設置上傳文件的最大尺寸1024字節=1K,這里是10K --> <property name="maxUploadSize"> <value>10240</value> </property> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean>
我們工程里面是這個問題引起的,但是我實際示例中發現超過大小是直接報錯的。
3.時間轉換問題
也有網友說是因為時間轉換引起的,而我實際操作中發現報錯是:
The server encountered an internal error that prevented it from fulfilling this request
這里也順便提一下,假如你的Controller要一個時間對象,代碼如下:
@SuppressWarnings("deprecation") @RequestMapping("/hello.do") public String hello(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="userName",defaultValue="佚名") String user, Date dateTest ){ request.setAttribute("user", user); System.out.println(dateTest.toLocaleString()); return "hello"; }
而網頁上實際給的是
<input type="text" name="dateTest" value="2015-06-07">
這里需要在Controller增加一個轉換器
@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); }
看完上述內容,你們掌握SpringMVC表單提交參數出現400錯誤的解決方法的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。