first, basic use, get the parameters of the submission
Back-End Code:
Java code
- @RequestMapping ("Testrequestparam")
- Public string Filesupload (@RequestParam string inputstr, HttpServletRequest request) {
- System.out.println (INPUTSTR);
- int inputint = integer.valueof (Request.getparameter ("Inputint"));
- System.out.println (Inputint);
- // ...... Omitted
- return "index";
- }
Front-End Code:
HTML code
- <form action="/gadget/testrequestparam" method="POST">
- Parameter inputstr:<input type="text" name= "inputstr">
- Parameter intputint:<input type="text" name= "inputint">
- </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
- @RequestParam String Inputstr
- The following pair of incoming parameters are specified as AA, if the front end does not pass AA parameter name, will error
- @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
- // required=false indicates that the argument will be assigned a value of null,required= True is required to have
- @RequestMapping (
- 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
- @RequestMapping ("Testrequestparam")
- Public string Filesupload (@RequestParam (value="AA", required=True) string inputstr,
- @RequestParam (value="Inputint", required=false) int inputint
- , HttpServletRequest request) {
- // ...... Omitted
- return "index";
- }
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.