furyfish furyfish - 2 years ago 246
Java Question

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

This is my controller:

@RequestMapping(value= "/create_customer", method = RequestMethod.POST)
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>();
Customer c = new Customer();
rs.setData("Insert success");
}catch(Exception ex){
rs.setData("Insert failure");
return rs.toString();

How can I resolved this?

Answer Source
  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 .

  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"
        <property name="maxUploadSize" value="250000"/>

Refer the Spring documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download