Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get param in method post spring mvc?

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?

like image 570
furyfish Avatar asked Jul 31 '13 08:07

furyfish


People also ask

What is the @RequestMapping annotation used for?

annotation. RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

Can we use @RequestParam in Get method?

Use the @RequestParam annotation to bind request parameters to a method parameter in your controller. AFAIK, request parameters are variables retrieved from query strings if the request method is GET. They are also the variables retrieved from the form values when the request method is POST.

Can we use @RequestBody and @RequestParam together?

nothing. So it fails with 400 because the request can't be correctly handled by the handler method.


2 Answers

  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.

like image 199
AllTooSir Avatar answered Oct 01 '22 05:10

AllTooSir


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") 
like image 42
Shamim Ahmmed Avatar answered Oct 01 '22 05:10

Shamim Ahmmed