如何在弹簧mvc后的方法中获得参数? [英] how to get param in method post spring mvc?

查看:103
本文介绍了如何在弹簧mvc后的方法中获得参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的表格:

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

这是我的控制器:

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

我如何解决这个问题?

推荐答案


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

@RequestParam(value="txtEmail", required=false)


  • 您甚至可以从请求对象中获取参数。

    request.getParameter(paramName);
    


  • 使用形成。这将很方便。 教程让您入门。

  • Use a form in case the number of attributes are large. It will be convenient. Tutorial to get you started.

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

    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>
    


  • 参考春季文档

    这篇关于如何在弹簧mvc后的方法中获得参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆