Spring MVC:错误400客户端发送的请求在语法上不正确
问题内容:
这似乎是一个普遍的问题。我已经解决了SO中给出的所有答案,但无法使其正常工作。
我正在尝试将Spring MVC +
Freemarker集成到已经存在的Web应用程序中。它可以很好地处理GET
请求,并且Freemarker模板可以毫无问题地读取Controller提供的java对象。
但是表单提交无法命中Controller方法。最后,我使log4j工作。这是我得到的 错误 :
错误
HandlerMethod details:
Controller [application.entry.controller.UserController]
Method [public void application.entry.controller.UserController.handleSave(java.lang.String)]
org.springframework.web.bind.MissingServletRequestParameterException:
Required String parameter 'action' is not present
Freemarker:
<form method="POST" action="save.html">
------------
<input type="submit" class="btnnew" name="saveWithoutValidation" value="Save Without Validation"></input>
<input type="submit" class="btnnew" name="submit" value="Submit"></input>
</form>
context-root是 PORTAL 。
spring-servlet.xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
web.xml
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
控制者
@RequestMapping(value="/save", method=RequestMethod.POST)
public void handleSave(@RequestParam String action){
if( action.equals("submit") ){
System.out.println("Damn! You clicked submit");
}
else if( action.equals("saveWithoutValidation") ){
System.out.println("Sweet! You want no string attached.");
}
}
对于日志,我尝试将其添加log4j.logger.org.springframework.web=DEBUG
到现有的log4j.properties中,但是没有用。
问题答案:
我也遇到了这个问题,我的解决方案也有所不同,因此为有类似问题的任何人添加此内容。
我的控制器有:
@RequestMapping(value = "/setPassword", method = RequestMethod.POST)
public String setPassword(Model model, @RequestParameter SetPassword setPassword) {
...
}
问题在于,这应该是 @ModelAttribute
针对对象的,而不是@RequestParameter
。此错误消息与您在问题中描述的相同。
@RequestMapping(value = "/setPassword", method = RequestMethod.POST)
public String setPassword(Model model, @ModelAttribute SetPassword setPassword) {
...
}