[英]spring http error 400 The request sent by the client was syntactically incorrect
[英]Spring MVC: Error 400 The request sent by the client was syntactically incorrect
这似乎是一个常见问题。 我已经完成了SO中给出的所有答案,但无法使其发挥作用。
我正在尝试将Spring MVC + Freemarker集成到现有的Web应用程序中。 它适用于GET
请求,Freemarker Template读取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,但它不起作用。
@RequestParam String action
表明请求中存在一个参数,其中名称操作在表单中不存在。 你必须:
<input name="action" />
@RequestParam
中将required参数设置为false
,例如@RequestParam(required=false)
我也有这个问题,我的解决方案也不同,所以在这里添加任何有类似问题的人。
我的控制器有:
@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) {
...
}
另一个可能的原因是RequestMapping属性的顺序错误。 正如春季医生所说:
@RequestMapping处理程序方法可以具有非常灵活的签名。 支持的方法参数和返回值将在下一节中介绍。 除了BindingResult参数之外,大多数参数可以以任意顺序使用 。 这将在下一节中介绍。
如果向下滚动文档,您将看到BindingResult必须立即在模型属性之后,因为每个请求可以有多个模型对象,因此可以有多个绑定
Errors或BindingResult参数必须遵循立即绑定的模型对象,因为方法签名可能有多个模型对象,Spring将为每个模型对象创建一个单独的BindingResult实例,因此以下示例将不起作用:
这是两个例子:
BindingResult和@ModelAttribute的排序无效。
@RequestMapping(method = RequestMethod.POST)public String processSubmit(@ModelAttribute(“pet”)Pet pet,Model model,BindingResult result){...}注意,Pet和BindingResult之间有一个Model参数。 要使其正常工作,您必须按如下方式重新排序参数:
@RequestMapping(method = RequestMethod.POST)public String processSubmit(@ModelAttribute(“pet”)Pet pet,BindingResult result,Model model){...}
我遇到了一个类似的问题,发现像Date这样的一些领域并没有得到具体的价值,一旦得到价值的东西工作得很好。 请确保您的表单上没有日期或任何其他需要具体值的字段。
根据错误:
Required String parameter 'action' is not present
需要在Spring的请求中存在一个名为action
的请求参数,以将请求映射到处理程序handleSave
。
您粘贴的HTML显示没有此类参数。
控制器试图在bean中找到“action”值,但根据你的例子,你没有设置任何bean名称“action”。 尝试做name =“action”。 @RequestParam总是在bean类中找到。
我的问题是我的模型属性后缺少BindingResult
参数。
@RequestMapping(method = RequestMethod.POST, value = "/sign-up", consumes = "application/x-www-form-urlencoded")
public ModelAndView registerUser(@Valid @ModelAttribute UserRegistrationInfo userRegistrationInfo
HttpServletRequest httpRequest,
HttpSession httpSession) { ... }
我添加了BindingResult
我的控制器就变成了
@RequestMapping(method = RequestMethod.POST, value = "/sign-up", consumes = "application/x-www-form-urlencoded")
public ModelAndView registerUser(@Valid @ModelAttribute UserRegistrationInfo userRegistrationInfo, BindingResult bindingResult,
HttpServletRequest httpRequest,
HttpSession httpSession) { ..}
检查答案@sashok_bg @sashko_bg Mersi mnogo
您的请求映射是/save
,但您的POST是/save.html
。 将POST更改为/save
应该修复它。
请保留你的
<form method="POST" action="XYZ">
@RequestMapping(value="/XYZ", method=RequestMethod.POST)
public void handleSave(@RequestParam String action){
您的表单操作属性值必须与@RequestMapping
值匹配,以便Spring MVC可以解析它。
另外,正如您所说的那样,在更改后给出了404,为此,请您检查控件是否进入handleSave()
方法。
我想,因为你没有从handleSave()
方法返回任何东西,你必须看看它。
如果它仍然不起作用,请你发布你的春季日志。
另外,请确保您的请求应该如此
/PORTAL/save
如果在PORTAL/jsp/save
之间有什么东西PORTAL/jsp/save
在@RequestMapping(value="/jsp/save")
的提及@RequestMapping(value="/jsp/save")
@CookieValue(value="abc",required=true) String m
当我将要求从真改变为假时,它就成功了。
在方法中添加BindingResult参数。 如需参考,请参阅下面的代码。
save(@ModelAttribute Employee employee,BindingResult bindingResult)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.