如何在弹簧mvc方法中获得参数?

fur*_*ish 38 java spring spring-mvc

我正在使用spring mvc.当method = post时,我无法从url获取param.但是当我将方法改为GET时,我可以得到所有的参数.

这是我的表格:

<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>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

@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();
}
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

NIN*_*OOP 51

  1. 如果删除,Spring注释将正常工作enctype="multipart/form-data".

    @RequestParam(value="txtEmail", required=false)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 您甚至可以从request对象中获取参数.

    request.getParameter(paramName);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 如果属性数量很大,请使用表单.这将很方便.帮助您入门的教程.

  4. 如果要接收,请配置多部件解析器enctype="multipart/form-data".

    <bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="250000"/>
    </bean>
    
    Run Code Online (Sandbox Code Playgroud)

请参阅Spring文档.


Sha*_*med 8

如果您更改内容类型,它也适用

    <form method="POST"
    action="http://localhost:8080/cms/customer/create_customer"
    id="frmRegister" name="frmRegister"
    enctype="application/x-www-form-urlencoded">
Run Code Online (Sandbox Code Playgroud)

在控制器中还添加标头值,如下所示:

    @RequestMapping(value = "/create_customer", method = RequestMethod.POST, headers = "Content-Type=application/x-www-form-urlencoded")
Run Code Online (Sandbox Code Playgroud)


Geo*_*lou 8

当我想获得所有POST参数时,我使用下面的代码,

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

我正在使用@RequestBodypost/put/delete http请求的注释,而不是@RequestParam读取GET参数.


Wil*_*era 7

您应该使用@RequestParam方法在这些资源上使用= RequestMethod.GET

要发布参数,您必须将它们作为请求正文发送.像JSON或其他数据表示形式的主体将取决于您的实现(我的意思是消费和生产MediaType).

通常,multipart/form-data用于上载文件.