SpringMVC表單提交參數(shù)400錯誤解決方案
SpringMVC下,提交表單報400錯:
description The request sent by the client was syntactically incorrect.
根據(jù)網(wǎng)上的總結(jié),可能是因為如下幾個問題引起的
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的屬性!
不想指定的話,你也可以定義這個屬性的默認值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">
<!-- 設置上傳文件的最大尺寸1024字節(jié)=1K,這里是10K -->
<property name="maxUploadSize">
<value>10240</value>
</property>
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
我們工程里面是這個問題引起的,但是我實際示例中發(fā)現(xiàn)超過大小是直接報錯的。
3.時間轉(zhuǎn)換問題
也有網(wǎng)友說是因為時間轉(zhuǎn)換引起的,而我實際操作中發(fā)現(xiàn)報錯是:
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)頁上實際給的是
<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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
如何在 Java 中利用 redis 實現(xiàn) LBS 服務
基于位置的服務,是指通過電信移動運營商的無線電通訊網(wǎng)絡或外部定位方式,獲取移動終端用戶的位置信息,在GIS平臺的支持下,為用戶提供相應服務的一種增值業(yè)務。下面我們來一起學習一下吧2019-06-06

