How to use the annotations of the SpringMVC framework?

SpringMVC parameter binding method

  • Basic data types, string data binding
  • Array type
  • vo type
  • list type
  • set type
  • Map type data
  • Custom composite type

Case practice

The binding of request parameters to the method parameters of the processor function processing method is very flexible for parameter binding

a). Basic data types, string data binding

/** 

* 简单数据类型 值必须存在 不传可以通过默认值代替 

*/ 

@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); 

} 

/** 

* 包装类型 值可以为空 

*/ 

@RequestMapping("data2") 

public void data2(Integer age,Double s){
    
     

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

} 

/** 

* 字符串注入 

* @param str 

*/ 

@RequestMapping("data3") 

public void data3(String str){
    
     

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

} 

b). Array type

@RequestMapping("/dataTest3") 

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

    for(String id:ids){
    
     

    	System.out.println(id+"---"); 

    } 

} 

c).vo type

@RequestMapping("/dataTest4") 

public void getParamsData4(User user){
    
     

	System.out.println(user); 

} 

d).list type

At this time, 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() {
    
     

    	return phones; 

    } 

    public void setPhones(List<Phone> phones) {
    
     

    	this.phones = phones; 

    } 

    public int getId() {
    
     

    	return id; 

    } 

    public void setId(int id) {
    
     

    	this.id = id; 

    } 

    public String getUserName() {
    
     

    	return userName; 

    } 

    public void setUserName(String userName) {
    
     

    	this.userName = userName; 

    } 

    public String getUserPwd() {
    
     

    	return userPwd; 

    } 

    public void setUserPwd(String userPwd) {
    
     

    	this.userPwd = userPwd; 

    } 

    @Override 

    public String toString() {
    
     

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

    \+ userPwd + ", phones=" + phones + "]"; 

    } 

} 

Phone entity

public class Phone {
    
     

    private String num; 

    public String getNum() {
    
     

    	return num; 

    } 

    public void setNum(String num) {
    
     

    	this.num = num; 

    } 

    @Override 

    public String toString() {
    
     

    	return "Phone [num=" + num + "]"; 

    } 

}

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> 

Controller method:

@RequestMapping("/dataTest5") 

public void getParamsData5(User user){
    
     

	System.out.println(user); 

} 

e).set type

​ Set is similar to List and needs to be bound to the object instead of being written directly in the parameters of the Controller method. However, when binding Set data, you must first add a corresponding number of model objects in the Set object.

Reasons for out-of-order storage

public class User {
    
     

    private int id; 

    private String userName; 

    private String userPwd; 

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

    public User() {
    
     

        phones.add(new Phone()); 

        phones.add(new Phone()); 

        phones.add(new Phone()); 

    } 

    public int getId() {
    
     

    	return id; 
        
    } 

    public void setId(int id) {
    
     

    this.id = id; 

    } 

    public String getUserName() {
    
     

    	return userName; 

    } 

    public void setUserName(String userName) {
    
     

    	this.userName = userName; 

    } 

    public String getUserPwd() {
    
     

    	return userPwd; 

    } 

    public void setUserPwd(String userPwd) {
    
     

    	this.userPwd = userPwd; 

    } 

    public Set<Phone> getPhones() {
    
     

    	return phones; 

    } 

    public void setPhones(Set<Phone> phones) {
    
     

    	this.phones = phones; 

    } 

}

Controller method:

@RequestMapping("/dataTest6") 

public void getParamsData6(User user){
    
     

	System.out.println(user); 

} 

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> 

f).Map type data

Map is the most flexible. It also needs to be bound to the object and cannot be written directly in the parameters of 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() {
    
     

        phones.add(new Phone()); 

        phones.add(new Phone()); 

        phones.add(new Phone()); 

    }  

    public int getId() {
    
     

    	return id; 

    } 

    public void setId(int id) {
    
     

    	this.id = id; 

    } 

    public String getUserName() {
    
     

    	return userName; 

    } 

    public void setUserName(String userName) {
    
     

    	this.userName = userName; 

    } 

    public String getUserPwd() {
    
     

    	return userPwd; 

    } 

    public void setUserPwd(String userPwd) {
    
     

    	this.userPwd = userPwd; 

    } 

    public Set<Phone> getPhones() {
    
     

    	return phones; 

    } 

    public void setPhones(Set<Phone> phones) {
    
     

    	this.phones = phones; 

    } 

    public Map<String, Phone> getMap() {
    
     

    	return map; 

    } 

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

    	this.map = map; 

    } 

}

Controller method:

@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()); 

    }  

} 

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> 

g). Custom composite type

The user entity refers to the Phone entity

public class User {
    
     

    private int id; 

    private String userName; 

    private String userPwd; 

    private Phone phone; 

    public int getId() {
    
     

    	return id; 

    } 

    public Phone getPhone() {
    
     

    	return phone; 

    } 

    public void setPhone(Phone phone) {
    
     

    	this.phone = phone; 

    } 

    public void setId(int id) {
    
     

    	this.id = id; 

    } 

    public String getUserName() {
    
     

    	return userName; 

    } 

    public void setUserName(String userName) {
    
     

    	this.userName = userName; 

    } 

    public String getUserPwd() {
    
     

    	return userPwd; 

    } 

    public void setUserPwd(String userPwd) {
    
     

    	this.userPwd = userPwd; 

    } 
}

Controller method:

@RequestMapping("/dataTest8") 

public void getParamsData8(User user){
    
     

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

} 

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> 

Extension~What is data verification?

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

Data verification is divided into client verification and server verification

Client verification: js verification

Server-side verification: springmvc uses verification verification, struts2 uses verification verification. Each has its own set of verification rules.

Guess you like

Origin blog.csdn.net/xyx12321/article/details/111595466
Recommended