闻道有先后

拔苗助长也得长,和Java死磕到底!

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对象的属性中去。

阅读更多
个人分类: 框架
想对作者说点什么? 我来说一句

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

不良信息举报

Spring MVC数据绑定(三)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭