1, directly writes the parameter in the controller corresponding method parameter, applies to the Get method, does not apply to the Post method
/** * 1. Directly write the parameters of the form in the formal parameter of the controller's corresponding method * @param username * @param password * @return /span> */ @RequestMapping ( "/adduser1" Span style= "COLOR: #000000" >) public String AddUser1 (string username,string password) {System.out.println ( "username is:" +username); SYSTEM.OUT.PRINTLN ( password is: "+password); return "Demo/index" ; }
URL form: The arguments submitted by http://localhost/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 need to match the name of the incoming parameter in the Controller method.
2, through the HttpServletRequest receive, the post way and get way can.
/*** 2, receive via HttpServletRequest *@paramRequest *@return */@RequestMapping ("/adduser2") Publicstring AddUser2 (HttpServletRequest request) {string Username=request.getparameter ("username"); String Password=request.getparameter ("Password"); System.out.println ("Username is:" +username); System.out.println ("Password is:" +password); return"Demo/index"; }
3, through a bean to receive, post and get method can be
1. Create a Bean object corresponding to the form to get
PackageDemo.model; Public classUsermodel {PrivateString username; PrivateString password; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } }
2. Use this bean to encapsulate the received parameters
/** * 3, through a bean to receive @param user @return * / @RequestMapping ("/adduser3") Public String addUser3 (usermodel user) { System.out.println ("username is:" +user.getusername ()); System.out.println ("password is:" +User.getpassword ()); return "Demo/index"; }
4.
to get the parameters in the path by @pathvariable
/** * 4, get the parameters in the path by @pathvariable * @param username * @param password * @return Span style= "COLOR: #008000" >*/ @RequestMapping (value = "/adduser4/{ Username}/{password} ", Method=requestmethod.get) public string addUser4 (@PathVariable string username, @PathVariable string password) {System . OUT.PRINTLN ( username is: "+username); SYSTEM.OUT.PRINTLN ( password is: "+password); return "Demo/index" ; }
For example, when accessing the http://localhost/SSMDemo/demo/addUser4/lixiaoxi/111111 path, the template variables {username} and {password} in the URL are automatically Bind to a parameter with the same name through the @pathvariable annotation, that is, after the entry Username=lixiaoxi, password=111111
5. use @modelattribute annotations to get the form form data for the POST request
<formAction= "<%=request.getcontextpath ()%>/demo/adduser5"Method= "POST">User name: <inputtype= "text"name= "User.username"/><BR/>Dense Code: <inputtype= "User.password"name= "Password"/><BR/> <inputtype= "Submit"value= "Submit"/> <inputtype= "Reset"value= "Reset"/> </form>
The Java controller is as follows
/** * 5. Use @modelattribute annotations to get the form form data for the POST request @param user @return * /@RequestMapping (Value= "/adduser5", method=requestmethod.post) public String addUser5 (@ModelAttribute ("user") Usermodel user) { System.out.println ( "username is:" +user.getusername ()); System.out.println ("password is:" +User.getpassword ()); return "Demo/index"; }
6. Binding request parameters to method @requestparam with annotations
An exception occurs when the request parameter username does not exist and can be resolved by setting the property Required=false, for example: @RequestParam (value= "username", Required=false)
/*** 6, with annotations @requestparam bind request parameters to method entry *@paramusername *@paramPassword *@return */@RequestMapping (Value= "/adduser6", method=requestmethod.get) PublicString AddUser6 (@RequestParam ("username") string username, @RequestParam ("Password") (String password) {System.out.println ("Username is:" +username); System.out.println ("Password is:" +password); return"Demo/index"; }
Originally from: https://www.cnblogs.com/xiaoxi/p/5695783.html
Receive different types of data in SPRINGMVC