There are two main ways to get the parameters in the Springmvc background control layer, one is Request.getparameter ("name") and the other is obtained directly with the annotation @requestparam. This is mainly about this note.

Source: Internet
Author: User

first, basic use, get the parameters of the submission
Back-End Code:

Java code
  1. @RequestMapping ("Testrequestparam")
  2. Public string Filesupload (@RequestParam string inputstr, HttpServletRequest request) {
  3. System.out.println (INPUTSTR);
  4. int inputint = integer.valueof (Request.getparameter ("Inputint"));
  5. System.out.println (Inputint);
  6. // ...... Omitted
  7. return "index";
  8. }



Front-End Code:

HTML code
  1. <form action="/gadget/testrequestparam" method="POST">
  2. Parameter inputstr:<input type="text" name= "inputstr">
  3. Parameter intputint:<input type="text" name= "inputint">
  4. </form>



Front-End Interface:


Execution Result:
Test1
123

You can see that spring is automatically encapsulated in the name of the parameter, and we can take the parameter name directly.

Second, various abnormal situation treatment
1. You can specify parameter names for incoming parameters

Java code
    1. @RequestParam String Inputstr
    2. The following pair of incoming parameters are specified as AA, if the front end does not pass AA parameter name, will error
    3. @RequestParam (value="AA") String inputstr


Error message:
HTTP Status 400-required String parameter ' AA ' is not present

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

Java code
    1. // required=false indicates that the argument will be assigned a value of null,required= True is required to have   
    2. @RequestMapping (
    3.      public string filesupload ( @RequestParam (Value= "AA",  required=true)  String inputStr,  httpservletrequest request)   



3, if the parameter annotated with @requestmapping is an int basic type, but required=false, then if the parameter value is not passed the error, because no value, will be assigned to the value of NULL int, this can not

Java code
  1. @RequestMapping ("Testrequestparam")
  2. Public string Filesupload (@RequestParam (value="AA", required=True) string inputstr,
  3. @RequestParam (value="Inputint", required=false) int inputint
  4. , HttpServletRequest request) {
  5. // ...... Omitted
  6. return "index";
  7. }



Workaround:
"Consider declaring it as Object wrapper for the corresponding primitive type." It is recommended that you use the wrapper type instead of the base type, such as "Integer" instead of "int"

There are two main ways to get the parameters in the Springmvc background control layer, one is Request.getparameter ("name") and the other is obtained directly with the annotation @requestparam. This is mainly about this note.

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.