Spring MVC数据绑定(三)

3.复合对象绑定

@Controller
@RequestMapping("/example3/schoolController")
public class schoolController {
	@RequestMapping("/bindSchool.htm")
	public String bindSchoiol(Model model,SchoolModel school){
		model.addAttribute("msg", school.toString());
		return "/example3/result";
	}
}

public class SchoolModel {

	private String schoolName;
	private int schoolId;
	private StudentModel student;
	
	public String getSchoolName() {
		return schoolName;
	}
	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
	}
	public int getSchoolId() {
		return schoolId;
	}
	public void setSchoolId(int schoolId) {
		this.schoolId = schoolId;
	}
	public StudentModel getStudent() {
		return student;
	}
	public void setStudent(StudentModel student) {
		this.student = student;
	}
	public String toString(){
		return "schoolModel[schoolName="+schoolName+",schoolId="+schoolId+",student="+student+"]";
		
	}

}

SchoolModel模型中包含了StudentModel,schoolModel为复合对象,在bindSchool()方法中,复合对象schoolModel为参数。

路径中schoolModel和schoolId的值自动注入到SchoolModel相应的属性中,而student.name和student.age注入到SchoolModel中的student对象的属性中去。

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页