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

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

问题描述

我正在使用 spring mvc.当method = post时,我无法从url获取参数.但是当我将方法更改为 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();
}

我该如何解决这个问题?

How can I resolved this?

推荐答案

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

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

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

  • 您甚至可以从 request 对象中获取参数.

    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>
    

  • 请参阅 Spring 文档.

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

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