How to get the view layer parameters in the controller layer of the SPRINGMVC

Source: Internet
Author: User

Method One:request.getparameter ("name")

Method Two: annotated @requestparam

@RequestMapping ("Testrequestparam")        // annotations in the way public     String Filesupload (@RequestParam (value= "Inputstr", Required=false) String inputstr, httpservletrequest request) {        System.out.println (INPUTSTR);         // Request.getparameter    int inputint = integer.valueof (Request.getparameter ("Inputint"));      System.out.println (inputint);             //  ...... Omit      return "index";     }    

Front-end Code

<formAction= "/gadget/testrequestparam"Method= "POST">parameter inputstr:<inputtype= "text"name= "Inputstr">parameter intputint:<inputtype= "text"name= "Inputint">    
<button> Submit </button></form>

Front-end Interface:


Execution Result:
Test1
123

----instructions are here----------------------------------------------------------------------

1.

1. ( fraught ): Write @requestparam directly, the request will be bound to the same name parameter on the method.

2. ( correct ): If the parameter name and variable name are inconsistent, you can use @requestparam (name) to tell spring to use the specified name to enter the parameter.

This is clearly wrong. The parameter name remains in the class file only when the debug mode is compiled, and spring can then reflect the binding. Our application is obviously not packaged in debug mode. So the first kind of fraught ah, the teacher told you very simple default value is wrong, the second way is correct.

2.

Required = True-the parameter must be passed

Required = False--can have no parameters and will assign null to value when no parameters



3, can be Required=false or true to require the @requestparam configuration of the front-end parameters must be transmitted

Java code
// Required=false indicates that the argument will be assigned a value of null,required=true is necessary to have a @RequestMapping ("Testrequestparam")          Public String Filesupload (@RequestParam (value= "AA", required=true) string inputstr, HttpServletRequest request)  

How to get the view layer parameters in the controller layer of the SPRINGMVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.