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