Binding request parameters SpringMVC

1, the binding request parameters described

1. binding mechanism: SpringMVC parameter binding process parameter request to the submitted form, a method as a parameter controller bundling

Submit the same name attribute parameters of the form and method of the controller: 2. requirements.

2, supported data types

1. The basic data types and string types

2.JavaBean

3. The aggregate data type

Example 1: Binding Type String

From the front end of the form "username" and "password" parameter controller in accordance with the method name!

 1 //---------------前端------------------
 2 <form action="user/login" method="get">
 3    用户名: <input type="txt" name="username">
 4    密  码: <input type="txt" name="password">
 5     <input type="submit" value="提交">
 6 </form>
 7  8 //---------------控制器------------------
 9 public String login(String username,String password){
10     System.out.println(
11             "用户名:"+username+"  密码:"+password
12     );
13     return "success";
14 }
Example 2, binding type JavaBean

Requirements: the attribute of the name of the bean from the front end of the object name form the value controller!

 1 //---------------javaBean------------------
 2 public class user {
 3     private String name;
 4     private String password;
 5 }
 6  7 //---------------前端------------------
 8 <form action="user/login" method="get">
 9    用户名: <input type="txt" name="name">
10    密  码: <input type="txt" name="password">
11     <input type="submit" value="提交">
12 </form>
15--------------- controller ------------------//1413 is     
 
 public String login(user newuser){
16     System.out.println(
17             "用户名:"+username+"  密码:"+password
18     );
19     return "success";
20 }
Example 3, the type of binding JavaBean (other references exist in the JavaBean JavaBean)

name value in the front end requires a reference to the object name. attribute bound form.

 1 //---------------javaBean(存在对user的引用)------------------
 2 public class Account {
 3     private String name;
 4     private String money;
 5     private user  newuser;
 6 }
 7 //---------------前端------------------
 8 <form action="user/adduser" method="post"  >
 9     账户名称: <input type="text" name="name"><br>
10     账户金额: <input type="text" name="money"><br>
11     用户名:<input type="text" name="newuser.name"><br>
12     用户密码:<input type="text" name="newuser.password"><br>
13     <input type="submit" value="提交">
14 </form>
15 //---------------控制器------------------
16     @RequestMapping("/adduser")
17     public String adduser(Account account){
18 19         System.out.println(account);
20         return "success";
21     }
22
Example 4, set binding type (List and Map)

List name attribute type using the front end of list [0] .name format, Map type using map [ 'one']. name format

. 1  // --------------- ------------------ the JavaBean 
2  public  class the Account {
 . 3      Private String name;
 . 4      Private String Money;
 . 5      Private List <User> List; // To demonstrate binding collection type 
. 6      Private the Map <String, User> Map; // To demonstrate binding collection type 
7  }
 8  // ----------- ---- ------------------ front end 
9 <% - binding test collection type -%>
 10 <form Action = "User / the adduser" Method = " POST ">
 11 account name: <the INPUT of the type =" text "name =" name "> <br>
 12The amount of accounts: <the INPUT of the type = "text" name = "Money"> <br>
 13 <% - to bind to a List collection -%>
 14 Nickname: <input type = "text" name = "list [0] .name "> <br>
 15 user password: <INPUT type =" text "name =" List [0] .password "> <br>
 16 <% - set to bind to the Map -% >
 17 nickname: <INPUT type = "text" name = "Map [ 'One'] name."> <br>
 18 is the user password: <input type = "text" name = "map [ 'one'] password. "> <br>
 19 <= the INPUT of the type" the submit "value =" submit ">
 20 </ form>
21 22 //---------------控制器------------------
23  @RequestMapping("/adduser")
24     public String adduser(Account account){
25 26         System.out.println(account);
27         return "success";
28     }

 

 

Guess you like

Origin www.cnblogs.com/lijie-helloworld/p/12460565.html