Acknowledgement: http://shawnccx.iteye.com/blog/730239
--------------------------------------------------
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 .
first, basic use, get the parameters of the submission
Back-End 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); // ...... Omit return "index";
Front-end code:
<formAction= "/gadget/testrequestparam"Method= "POST">parameter inputstr:<inputtype= "text"name= "Inputstr">parameter intputint:<inputtype= "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
@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
// Required=false means that if you do not preach, you will assign a value of Null,required=true to a parameter. @RequestMapping ("Testrequestparam") 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
@RequestMapping ("Testrequestparam") public String filesupload (@RequestParam (value= "AA", Required=true) String inputstr, @RequestParam (Value= "Inputint", required=false int inputint , httpservletrequest request) { // ... Omit 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"
SPRINGMVC annotation @requestparam (EXT)