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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用線程池實現(xiàn)socket編程的方法詳解
這篇文章主要為大家詳細介紹了Java使用線程池實現(xiàn)socket編程的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
MyBatis Plus整合Redis實現(xiàn)分布式二級緩存的問題
Mybatis內(nèi)置的二級緩存在分布式環(huán)境下存在分布式問題,無法使用,但是我們可以整合Redis來實現(xiàn)分布式的二級緩存,這篇文章給大家介紹MyBatis Plus整合Redis實現(xiàn)分布式二級緩存,感興趣的朋友跟隨小編一起看看吧2023-11-11
深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)
通常來講,重構(gòu)是指不改變功能的情況下優(yōu)化代碼,但本文所說的重構(gòu)也包括了添加功能。這篇文章主要介紹了重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)的相關(guān)資料,需要的朋友可以參考下2016-11-11
基于springboot實現(xiàn)一個簡單的aop實例
這篇文章主要介紹了基于springboot實現(xiàn)一個簡單的aop,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11
Java socket通信模擬QQ實現(xiàn)多人聊天室
Socket在Java實戰(zhàn)網(wǎng)絡通信編程應用中有非常重要的作用,你想要跟別人聯(lián)系都得通過socket占據(jù)端口來實現(xiàn),掌握Socket技術(shù)不僅在聊天應用程序中需要用到(比如QQ什么的都都是用socket來寫的),而且對于學習 Asp.net 也非常有幫助2022-07-07

