【问题标题】:how to get param in method post spring mvc?如何在spring mvc之后的方法中获取参数?
【发布时间】:2013-07-31 15:38:17
【问题描述】:

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

这是我的控制器:

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

我该如何解决这个问题?

【问题讨论】:

  • 您是如何在 JSP 的表单中设置这些参数的?你能再展示一些代码吗?

标签: java spring spring-mvc


【解决方案1】:
  1. 如果您删除 enctype="multipart/form-data",Spring 注释将正常工作。

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

    request.getParameter(paramName);
    
  3. 如果属性数量很大,请使用form。会很方便。 Tutorial 让您开始。

  4. 如果您想接收enctype="multipart/form-data",请配置多部分解析器。

    <bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="250000"/>
    </bean>
    

请参阅Spring documentation

【讨论】:

  • 如果参数过多,我建议使用表单来绑定参数。 Spring 的 @RequestParam 注释是一种便利机制,它绝对比在整个代码中使用 request.getParameter 更好。但是为 MultipartResolver 位 +1。
  • 如果使用表单标签,html代码中的bean字段名称是否无关紧要?
  • multipartResolver 对我不起作用。我仍然得到同样的错误。除了添加 bean 定义之外,我还需要做其他事情吗?我是否需要在我的控制器类/方法中添加一些东西才能使用它?
【解决方案2】:

如果您更改内容类型,它也可以工作

    <form method="POST"
    action="http://localhost:8080/cms/customer/create_customer"
    id="frmRegister" name="frmRegister"
    enctype="application/x-www-form-urlencoded">

在控制器中还添加header值如下:

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

【讨论】:

    【解决方案3】:

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

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

    我将 @RequestBody 注释用于 post/put/delete http 请求,而不是读取 GET 参数的 @RequestParam

    【讨论】:

    • 谢谢,这就是我想要的……!!
    【解决方案4】:

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

    为了发布参数,您必须将它们作为请求正文发送。 JSON 之类的主体或其他数据表示形式将取决于您的实现(我的意思是,使用和生成 MediaType)。

    通常, multipart/form-data 用于上传文件。

    【讨论】:

      猜你喜欢
      • 2014-10-23
      • 1970-01-01
      • 2016-06-06
      • 2018-07-06
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多