天天看点

springMVC ModelAttribute使用说明

1. ModelAttribute 绑定对象中的集合属性

spring提供简单的对象绑定,当然支持集合对象绑定了。不过你要定义一个对象,属性有一个集合,集合里存放Person对象。

比如

public class PersonList {  
	  
    private List<Person> person;  
  
    public List<Person> getPerson() {  
        return person;  
    }  
  
    public void setPerson(List<Person> person) {  
        this.Person= person;  
    }  
  
}  


controller里:
@RequestMapping(value = "xxx", method = RequestMethod.POST)
public void test(PersonList person) {
		//遍历person 
	    }  		

页面要这样写:
<form id="form" action="test/test.do" method="post">
<input type="text" name="person[0].username" value="jobs"/>
<input type="text" name="person[0].age" value="55"/><br/>
<input type="text" name="person[1].username]" value="jim"/>
<input type="text" name="person[1].age" value="21"/><br/>
<input type="submit">
<form>
           

2.ModelAttribute绑定表单日期类型的参数,注意要返回日期格式的字符串,不要用number类型的数字

<input type="datetime" name="startClassTime" id="startClassTime" value="2016-10-01 15:45:30">  
           

若无法正常绑定属性,可添加

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")   
private Date startClassTime;