- 2023-01-28 12:57:26
- 2669 热度
- 0 评论
SpringMVC下,提交表单报400错:
description The request sent by the client was syntactically incorrect.
根据网上的总结,可能是因为如下几个问题引起的
参数指定问题
如果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"; }
上传问题
上传文件大小超出了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>
我们工程里面是这个问题引起的,但是我实际示例中发现超过大小是直接报错的。
时间转换问题
也有网友说是因为时间转换引起的,而我实际操作中发现报错是:
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)); }
如有其他因素,欢迎分享。
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- MVC(22)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- Web(21)
- CLI(20)
- SpringMVC(19)
- Alibaba(19)
- SpringBoot(17)
- Docker(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- ORA(15)
- JPA(15)
- Apache(15)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- Data(12)
- JSON(12)
- OAuth2(12)
- stream(11)
- int(11)
- Myeclipse(11)
- Bug(10)
- not(10)
- ast(9)
- maven(9)
- Map(9)
- Hystrix(9)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- HTML(7)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- windows(7)
- too(7)
- RabbitMQ(6)
- and(6)
- star(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- nginx(6)
- Server(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- JWT(5)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- Syntaxhighlighter(5)
- script(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)