how to get param in method post spring mvc?

后端 未结 4 1875
执笔经年
执笔经年 2020-11-29 23:59

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:

         


        
相关标签:
4条回答
  • 2020-11-30 00:26
    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 .

      request.getParameter(paramName);
      
    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"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          <property name="maxUploadSize" value="250000"/>
      </bean>
      

    Refer the Spring documentation.

    0 讨论(0)
  • 2020-11-30 00:33

    You should use @RequestParam on those resources with method = RequestMethod.GET

    In order to post parameters, you must send them as the request body. A body like JSON or another data representation would depending on your implementation (I mean, consume and produce MediaType).

    Typically, multipart/form-data is used to upload files.

    0 讨论(0)
  • 2020-11-30 00:35

    It also works if you change the content type

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

    In the controller also add the header value as follows:

        @RequestMapping(value = "/create_customer", method = RequestMethod.POST, headers = "Content-Type=application/x-www-form-urlencoded")
    
    0 讨论(0)
  • 2020-11-30 00:35

    When I want to get all the POST params I am using the code below,

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

    I am using the @RequestBody annotation for post/put/delete http requests instead of the @RequestParam which reads the GET parameters.

    0 讨论(0)
提交回复
热议问题