42

I'm using spring mvc. And I can't get param from url when method = post. But when I change method to GET, so I can get all param.

This is my form:

<form method="POST" action="http://localhost:8080/cms/customer/create_customer" id="frmRegister" name ="frmRegister" enctype="multipart/form-data">
    <input class ="iptRegister" type="text" id="txtEmail" name="txtEmail" value="" />
    <input class ="iptRegister" type="password" id="txtPassword" name="txtPassword" value="" />
    <input class ="iptRegister" type="text" id="txtPhone" name="txtPhone" value="" />

    <input type="button" id="btnRegister" name="btnRegister" value="Register" onclick="" style="cursor:pointer"/>
</form>

This is my controller:

@RequestMapping(value= "/create_customer", method = RequestMethod.POST)
@ResponseBody
public String createCustomer(HttpServletRequest request, 
        @RequestParam(value="txtEmail", required=false) String email, 
        @RequestParam(value="txtPassword", required=false) String password, 
        @RequestParam(value="txtPhone", required=false) String phone){

    ResultDTO<String> rs = new ResultDTO<String>();
    rs.setStatus(IConfig.SHOW_RESULT_SUCCESS_ON_MAIN_SCREEN);
    try{
        Customer c = new Customer();
        c.setEmail(email);
        c.setPassword(password);
        c.setPhone(phone);
        customerService.insert(c);
        rs.setData("Insert success");
    }catch(Exception ex){
        log.error(ex);
        rs.setStatus(IConfig.SHOW_RESULT_ERROR_ON_MAIN_SCREEN);
        rs.setData("Insert failure");
    }
    return rs.toString();
}

How can I resolved this?

Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
furyfish
  • 2,055
  • 5
  • 26
  • 28
  • 2
    How are you setting those parameters in the form in your JSP? Can you show some more of that code? – adarshr Jul 31 '13 at 08:07

4 Answers4

53
  1. Spring annotations will work fine if you remove enctype="multipart/form-data".

    @RequestParam(value="txtEmail", required=false)
    
  2. You can even get the parameters from the request object .

    request.getParameter(paramName);
    
  3. Use a form in case the number of attributes are large. It will be convenient. Tutorial to get you started.

  4. Configure the Multi-part resolver if you want to receive enctype="multipart/form-data".

    <bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="250000"/>
    </bean>
    

Refer the Spring documentation.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • I would recommend using a form for binding the parameters if there are more than a few of them. Spring's `@RequestParam` annotation is a convenience mechanism which definitely reads better than doing `request.getParameter` throughout the code. But +1 for the `MultipartResolver` bit. – adarshr Jul 31 '13 at 08:12
  • In case of using form tag, doesn't matter to have the bean fields names in the html code? – gab06 Nov 20 '15 at 19:51
  • The multipartResolver didn't work for me. I still get the same error. Do I need to do anything else besides add the bean definition? Do I need to add something to my controller class/method to use it? – JoeMjr2 Jul 11 '18 at 20:46
8

It also works if you change the content type

    <form method="POST"
    action="http://localhost:8080/cms/customer/create_customer"
    id="frmRegister" name="frmRegister"
    enctype="application/x-www-form-urlencoded">

In the controller also add the header value as follows:

    @RequestMapping(value = "/create_customer", method = RequestMethod.POST, headers = "Content-Type=application/x-www-form-urlencoded")
Shamim Ahmmed
  • 8,265
  • 6
  • 25
  • 36
8

When I want to get all the POST params I am using the code below,

@RequestMapping(value = "/", method = RequestMethod.POST)
public ViewForResponseClass update(@RequestBody AClass anObject) {
    // Source..
}

I am using the @RequestBody annotation for post/put/delete http requests instead of the @RequestParam which reads the GET parameters.

Jason Krs
  • 704
  • 2
  • 9
  • 30
Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
7

You should use @RequestParam on those resources with method = RequestMethod.GET

In order to post parameters, you must send them as the request body. A body like JSON or another data representation would depending on your implementation (I mean, consume and produce MediaType).

Typically, multipart/form-data is used to upload files.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Wilder Valera
  • 986
  • 9
  • 11