您好,登錄后才能下訂單哦!
SpringMVC表單提交參數(shù)出現(xiàn)400錯(cuò)誤的解決方法?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問題。
1.參數(shù)指定問題
如果Controller中定義了參數(shù),而表單內(nèi)卻沒有定義該字段
@SuppressWarnings("deprecation") @RequestMapping("/hello.do") public String hello(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="userName") String user ){ request.setAttribute("user", user); return "hello"; }
這里,表單內(nèi)必須提供一個(gè)userName的屬性!
不想指定的話,你也可以定義這個(gè)屬性的默認(rèn)值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"; }
也可以指定該參數(shù)是非必須的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"> <!-- 設(shè)置上傳文件的最大尺寸1024字節(jié)=1K,這里是10K --> <property name="maxUploadSize"> <value>10240</value> </property> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean>
我們工程里面是這個(gè)問題引起的,但是我實(shí)際示例中發(fā)現(xiàn)超過(guò)大小是直接報(bào)錯(cuò)的。
3.時(shí)間轉(zhuǎn)換問題
也有網(wǎng)友說(shuō)是因?yàn)闀r(shí)間轉(zhuǎn)換引起的,而我實(shí)際操作中發(fā)現(xiàn)報(bào)錯(cuò)是:
The server encountered an internal error that prevented it from fulfilling this request
這里也順便提一下,假如你的Controller要一個(gè)時(shí)間對(duì)象,代碼如下:
@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"; }
而網(wǎng)頁(yè)上實(shí)際給的是
<input type="text" name="dateTest" value="2015-06-07">
這里需要在Controller增加一個(gè)轉(zhuǎn)換器
@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); }
看完上述內(nèi)容,你們掌握SpringMVC表單提交參數(shù)出現(xiàn)400錯(cuò)誤的解決方法的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。