Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: Error 400 The request sent by the client was syntactically incorrect

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


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 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>

Controller

@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.");
        }

    }

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

like image 388
Himanshu Yadav Avatar asked Jul 22 '13 16:07

Himanshu Yadav


1 Answers

Another possible cause is to have the wrong order of RequestMapping attributes. As spring doc says:

An @RequestMapping handler method can have a very flexible signatures. The supported method arguments and return values are described in the following section. Most arguments can be used in arbitrary order with the only exception of BindingResult arguments. This is described in the next section.

If you scroll down the doc, you will see that the BindingResult has to be immediatelly after the model attribute, since we can have multiple model objects per request and thus multiple bindings

The Errors or BindingResult parameters have to follow the model object that is being bound immediately as the method signature might have more than one model object and Spring will create a separate BindingResult instance for each of them so the following sample won’t work:

Here are two examples:

Invalid ordering of BindingResult and @ModelAttribute.

@RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("pet") Pet pet, Model model, BindingResult result) { ... } Note, that there is a Model parameter in between Pet and BindingResult. To get this working you have to reorder the parameters as follows:

@RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, Model model) { ... }

like image 68
sashok_bg Avatar answered Sep 23 '22 21:09

sashok_bg