Spring MVC:错误400客户端发送的请求在语法上不正确 [英] Spring MVC: Error 400 The request sent by the client was syntactically incorrect

查看:129
本文介绍了Spring MVC:错误400客户端发送的请求在语法上不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个常见问题.我已经解决了SO中给出的所有答案,但无法使它起作用.
我正在尝试将Spring MVC + Freemarker集成到已经存在的Web应用程序中.它对于GET请求正常工作,并且Freemarker模板可以毫无问题地读取Controller提供的java对象.
但是表单提交无法命中Controller方法.最后,我使log4j正常工作.这是我遇到的错误:
错误

This seems to be a common issue. I have gone all the answers given in SO but could not make it work.
I am trying to integrate Spring MVC+Freemarker in already existing web application. It works fine for the GET request and Freemarker Template reads java object provided by Controller without any issue.
But Form submission is not able to hit Controller method. Finally I made log4j work. Here is the error I am getting:
Error

    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


免费标记:

<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

context-root is 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中,但是它不起作用.

For logs I have tried to add log4j.logger.org.springframework.web=DEBUG to my existing log4j.properties but it didn't work.

推荐答案

我也遇到了这个问题,我的解决方案也有所不同,因此,请为有类似问题的任何人添加此处.

I also had this issue and my solution was different, so adding here for any who have similar problem.

我的控制器有:

@RequestMapping(value = "/setPassword", method = RequestMethod.POST)
public String setPassword(Model model, @RequestParameter SetPassword setPassword) {
    ...
}

问题在于该对象应为 @ModelAttribute ,而不是@RequestParameter.错误消息与您在问题中描述的相同.

The issue was that this should be @ModelAttribute for the object, not @RequestParameter. The error message for this is the same as you describe in your question.

@RequestMapping(value = "/setPassword", method = RequestMethod.POST)
public String setPassword(Model model, @ModelAttribute SetPassword setPassword) {
    ...
}

这篇关于Spring MVC:错误400客户端发送的请求在语法上不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆