How SpringMVC parameters are bound

  • Basic data type, string data binding
  • An array type
  • Vo type
  • List the type
  • The set type
  • Map data
  • Custom compound types

A case in field

The binding of request parameters to the method parameters of the handler function processing method, which is flexible for parameter binding

A). Basic data type, string data binding

/** * Simple data type values must exist 

@RequestMapping("data1") 

public void data1(@RequestParam(defaultValue="10",name="age")int age, 
@RequestParam(defaultValue="1",name="flag")boolean flag, 
@RequestParam(defaultValue="100",name="s")double s){ 

	System.err.println("age:"+age+":flag:"+flag+":s:"+s); 

} 

/** * The wrapper type value can be empty */ 

@RequestMapping("data2") 

public void data2(Integer age,Double s){ 

	System.err.println("age:"+age+":s:"+s); 

} 

/** * string injection *@param str 

*/ 

@RequestMapping("data3") 

public void data3(String str){ 

	System.err.println("str:"+str); 

} 
Copy the code

B). Array type

@RequestMapping("/dataTest3") 

public void getParamsData3(@RequestParam(value="ids")String[] ids){ 

    for(String id:ids){ 

    	System.out.println(id+"-"); }}Copy the code

C) vo type

@RequestMapping("/dataTest4") 

public void getParamsData4(User user){ 

	System.out.println(user); 

} 
Copy the code

D). The list type

The user entity needs to define the list attribute

import java.util.ArrayList; 

import java.util.List; 

public class User { 

    private int id; 

    private String userName; 

    private String userPwd; 

    private List<Phone> phones=new ArrayList<Phone>(); 

    public List<Phone> getPhones(a) { 

    	return phones; 

    } 

    public void setPhones(List<Phone> phones) { 

    	this.phones = phones; 

    } 

    public int getId(a) { 

    	return id; 

    } 

    public void setId(int id) { 

    	this.id = id; 

    } 

    public String getUserName(a) { 

    	return userName; 

    } 

    public void setUserName(String userName) { 

    	this.userName = userName; 

    } 

    public String getUserPwd(a) { 

    	return userPwd; 

    } 

    public void setUserPwd(String userPwd) { 

    	this.userPwd = userPwd; 

    } 

    @Override 

    public String toString(a) { 

    	return "User [id=" + id + ", userName=" + userName + ", userPwd=" 

    \+ userPwd + ", phones=" + phones + "]"; }}Copy the code

Phone entity

public class Phone { 

    private String num; 

    public String getNum(a) { 

    	return num; 

    } 

    public void setNum(String num) { 

    	this.num = num; 

    } 

    @Override 

    public String toString(a) { 

    	return "Phone [num=" + num + "]"; }}Copy the code

Jsp page definition

<form action="dataTest5.do" method="post"> 

    <input name="phones[0].num" value="123456" /> 

    <input name="phones[1].num" value="4576" /> 

    <button type="submit"</button> </form>Copy the code

The Controller methods:

@RequestMapping("/dataTest5") 

public void getParamsData5(User user){ 

	System.out.println(user); 

} 
Copy the code

E). The set type

A set, like a List, needs to be bound to an object rather than written as a parameter to the Controller method. However, to bind Set data, you must first add a corresponding number of model objects to the Set.

Cause of unordered storage

public class User { 

    private int id; 

    private String userName; 

    private String userPwd; 

    private Set<Phone> phones=new HashSet<Phone>(); 

    public User(a) { 

        phones.add(new Phone()); 

        phones.add(new Phone()); 

        phones.add(new Phone()); 

    } 

    public int getId(a) { 

    	return id; 
        
    } 

    public void setId(int id) { 

    this.id = id; 

    } 

    public String getUserName(a) { 

    	return userName; 

    } 

    public void setUserName(String userName) { 

    	this.userName = userName; 

    } 

    public String getUserPwd(a) { 

    	return userPwd; 

    } 

    public void setUserPwd(String userPwd) { 

    	this.userPwd = userPwd; 

    } 

    public Set<Phone> getPhones(a) { 

    	return phones; 

    } 

    public void setPhones(Set<Phone> phones) { 

    	this.phones = phones; }}Copy the code

The Controller methods:

@RequestMapping("/dataTest6") 

public void getParamsData6(User user){ 

	System.out.println(user); 

} 
Copy the code

The form page

<form action="dataTest6.do" method="post"> 

    <input name="phones[0].num" value="123456" /> 

    <input name="phones[1].num" value="4576" /> 

    <input name="phones[2].num" value="4576" /> 

    <button type="submit"</button> </form>Copy the code

F).Map data

A Map is the most flexible, and it also needs to be bound to an object rather than written directly as a parameter to the Controller method.

public class User { 

    private int id; 

    private String userName; 

    private String userPwd; 

    private Set<Phone> phones=new HashSet<Phone>(); 

    private Map<String, Phone> map=new HashMap<String, Phone>(); 

	public User(a) { 

        phones.add(new Phone()); 

        phones.add(new Phone()); 

        phones.add(new Phone()); 

    }  

    public int getId(a) { 

    	return id; 

    } 

    public void setId(int id) { 

    	this.id = id; 

    } 

    public String getUserName(a) { 

    	return userName; 

    } 

    public void setUserName(String userName) { 

    	this.userName = userName; 

    } 

    public String getUserPwd(a) { 

    	return userPwd; 

    } 

    public void setUserPwd(String userPwd) { 

    	this.userPwd = userPwd; 

    } 

    public Set<Phone> getPhones(a) { 

    	return phones; 

    } 

    public void setPhones(Set<Phone> phones) { 

    	this.phones = phones; 

    } 

    public Map<String, Phone> getMap(a) { 

    	return map; 

    } 

    public void setMap(Map<String, Phone> map) { 

    	this.map = map; }}Copy the code

The Controller methods:

@RequestMapping("/dataTest7") 

public void getParamsData7(User user){ 

    Set<Entry<String, Phone>> set=user.getMap().entrySet(); 

    for(Entry<String, Phone> entry:set){ 

    	System.out.println(entry.getKey()+"--"+entry.getValue().getNum()); }}Copy the code

The form page

<form action="dataTest7.do" method="post"> 

    <input name="map['1'].num" value="123456" /> 

    <input name="map['2'].num" value="4576" /> 

    <input name="map['3'].num" value="4576" /> 

    <button type="submit"</button> </form>Copy the code

G). Custom compound types

The user entity references the Phone entity

public class User { 

    private int id; 

    private String userName; 

    private String userPwd; 

    private Phone phone; 

    public int getId(a) { 

    	return id; 

    } 

    public Phone getPhone(a) { 

    	return phone; 

    } 

    public void setPhone(Phone phone) { 

    	this.phone = phone; 

    } 

    public void setId(int id) { 

    	this.id = id; 

    } 

    public String getUserName(a) { 

    	return userName; 

    } 

    public void setUserName(String userName) { 

    	this.userName = userName; 

    } 

    public String getUserPwd(a) { 

    	return userPwd; 

    } 

    public void setUserPwd(String userPwd) { 

    	this.userPwd = userPwd; }}Copy the code

The Controller methods:

@RequestMapping("/dataTest8") 

public void getParamsData8(User user){ 

    System.out.println(user.getUserName()+"-"+user.getUserPwd()+"-"+user.getPhone().getNum()); 

} 
Copy the code

The form page

<form action="dataTest8.do" method="post"> 

    <input name="userName" value="123456" /> 

    <input name="userPwd" value="4576" /> 

    <input name="phone.num" value="4576" /> 

    <button type="submit"</button> </form>Copy the code

Extension ~ What is data validation?

This is used to verify whether the data entered by the customer is valid. For example, when the customer logs in, the user name cannot be empty or exceed the specified length. This is called data verification.

Data verification includes client verification and server verification

Client verification: JS verification

Server-side validation: SpringMVC uses Validation validation, struts2 uses validation validation. They all have their own set of verification rules.