How Spring MVC passes values from a page to a controller
There are many ways to transfer values from a page to a controller, and this article deals with several common ways to transfer values from a page to a controller:
1. Passing data to the background through HttpServletRequest
The page program is as follows:
<H2>Data transmission using HttpServletRequest</H2> <formAction= "Logintojson">User name:<inputname= "Name" />Password:<inputname= "Pass" /> <inputtype= "Submit"value= "Login"> </form>
View Code
The background program section is as follows:
@ResponseBody @RequestMapping (Value= "/logintojson", method=requestmethod.post) Public String Logintojson (httpservletrequest request) { new User (); User.setusername (Request.getparameter ("name")); User.setpassword (Request.getparameter ("pass")); return json.tojsonstring (user); }
View Code
2. Passing values to the controller via parameters
The foreground program is as follows:
<H2>Data transmission using Function parameters</H2> <formAction= "Loginbyvariable"Method= "POST">User name:<inputname= "Name" />Password:<inputname= "Pass" /> <inputtype= "Submit"value= "Login"> </form>
View Code
The background program is as follows:
@RequestMapping (value= "/loginbyvariable", method=requestmethod.post) public String Logintosession (httpservletrequest request,modelmap map,string name,string pass) { new User (); User.setusername (name); User.setpassword (pass); Map.put ("user", user); SYSTEM.OUT.PRINTLN (user); return "MySession"; }
View Code
Note: The value of the Name property in the foreground <input name= "name"/> must be the same as the parameter name in string name in the background function argument, otherwise the value will not be taken from the background.
3, using the function parameter, the object passes the value to the controller
front-facing conditions , the definition code for the object:
Note here that the properties of the user object are named username and password, and have getter and setter properties
Public classuser{@Override PublicString toString () {return"User [username=" + UserName + ", password=" + password + "]"; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PrivateString UserName; PrivateString password;}
View Code
The foreground program is as follows:
Note: userName and password must match the object property name.
<H2>Data transmission using function parameter objects</H2> <formAction= "Loginbyobject"Method= "POST">User name:<inputname= "UserName" />Password:<inputname= "Password" /> <inputtype= "Submit"value= "Login"></form>
View Code
The background program is as follows:
@RequestMapping (value= "/loginbyobject", method=requestmethod.post) public String Logintosession (modelmap map,user user) { new User (); User1.setusername (User.getusername ()); User1.setpassword (User.getpassword ()); System.out.println (user1+ "Object"); Map.put ("user", user1); return "MySession";
View Code
4, using the function parameter @requestparam parameter transfer according to
The front code is as follows:
<BR> <H2>Using function parameters to @requestparam parameters</H2> <formAction= "Loginbyrequestparm"Method= "POST">User name:<inputname= "Name" />Password:<inputname= "Pass" /> <inputtype= "Submit"value= "Login"> </form>
View Code
The background program is as follows:
@RequestMapping (value= "/loginbyrequestparm", method=requestmethod.post) public String Logintosessiona (httpservletrequest request,modelmap map, @RequestParam ("name") String username,@ Requestparam String Pass) { new User (); User.setusername (UserName); User.setpassword (pass); Map.put ("user", user); System.out.println (user+ "Requestparam"); return "MySession"; }
View Code
Note: If there are parameters in the @RequestParam ("name"), name must be the value of the property in the page, and if there are no parameters, the parameter name must be the same as the value of the property on the page, such as pass in the page, in @requestparam, you can specify no parameters. But the name of the formal parameter must be pass.
5. Pass parameters through URL @PathVariable
The specific code is as follows:
@Controller Public classTestController {@RequestMapping (value= "/user/{userid}/roles/{roleid}", method =requestmethod.get) PublicString GetLogin (@PathVariable ("UserId") String userId, @PathVariable ("Roleid") (String Roleid) {System.out.println ("User Id:" +userId); System.out.println ("Role Id:" +Roleid); return"Hello"; } @RequestMapping (Value= "/product/{productid}", method =requestmethod.get) PublicString getproduct (@PathVariable ("ProductId") (String productId) {System.out.println ("Product Id:" +productId); return"Hello"; } @RequestMapping (Value= "/javabeat/{regexp1:[a-z-]+}", Method=requestmethod.get) PublicString Getregexp (@PathVariable ("Regexp1") (String regexp1) {System.out.println ("URI Part 1:" +REGEXP1); return"Hello"; } }
View Code
How Spring MVC passes parameters from a page to a controller