SpringMVC表單提交參數(shù)400錯誤解決方案
SpringMVC下,提交表單報(bào)400錯:
description The request sent by the client was syntactically incorrect.
根據(jù)網(wǎng)上的總結(jié),可能是因?yàn)槿缦聨讉€問題引起的
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)必須提供一個userName的屬性!
不想指定的話,你也可以定義這個屬性的默認(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>
我們工程里面是這個問題引起的,但是我實(shí)際示例中發(fā)現(xiàn)超過大小是直接報(bào)錯的。
3.時間轉(zhuǎn)換問題
也有網(wǎng)友說是因?yàn)闀r間轉(zhuǎn)換引起的,而我實(shí)際操作中發(fā)現(xiàn)報(bào)錯是:
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"; }
而網(wǎng)頁上實(shí)際給的是
<input type="text" name="dateTest" value="2015-06-07">
這里需要在Controller增加一個轉(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- layui 圖片上傳+表單提交+ Spring MVC的實(shí)例
- SpringMVC實(shí)現(xiàn)表單驗(yàn)證功能詳解
- springMVC中基于token防止表單重復(fù)提交方法
- SpringMVC中使用bean來接收form表單提交的參數(shù)時的注意點(diǎn)
- SpringMVC實(shí)現(xiàn)數(shù)據(jù)綁定及表單標(biāo)簽
- Spring MVC---數(shù)據(jù)綁定和表單標(biāo)簽詳解
- [Spring MVC] -簡單表單提交實(shí)例
- SpringMVC處理Form表單實(shí)例
- Spring MVC中基于自定義Editor的表單數(shù)據(jù)處理技巧分享
相關(guān)文章
如何在 Java 中利用 redis 實(shí)現(xiàn) LBS 服務(wù)
基于位置的服務(wù),是指通過電信移動運(yùn)營商的無線電通訊網(wǎng)絡(luò)或外部定位方式,獲取移動終端用戶的位置信息,在GIS平臺的支持下,為用戶提供相應(yīng)服務(wù)的一種增值業(yè)務(wù)。下面我們來一起學(xué)習(xí)一下吧2019-06-06Java基礎(chǔ)入門篇之邏輯控制練習(xí)題與猜數(shù)字游戲
猜數(shù)字游戲是一款經(jīng)典的游戲,該游戲說簡單也很簡單,說不簡單確實(shí)也很難,這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)入門篇之邏輯控制練習(xí)題與猜數(shù)字游戲的相關(guān)資料,需要的朋友可以參考下2023-06-06