SpringMVC处理模型数据——ModelAndView
2019-03-04 本文已影响0人
花无缺_0159
如果跳转时需要带数据:视图、模型,那么可以使用ModelAndView,自动将对象放入request域中。
一个例子:
实体类Student.java:
package com.test.entity;
public class Student {
private int id;
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private Address address ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address.java:
package com.test.entity;
public class Address {
private String homeAddress ;
private String schoolAddress ;
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getSchoolAddress() {
return schoolAddress;
}
public void setSchoolAddress(String schoolAddress) {
this.schoolAddress = schoolAddress;
}
}
首页index.jsp(发送请求):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<br/>
<a href="handler/testModelAndView">testModelAndView</a>
<br/>
</body>
</html>
跳转后的页面success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${requestScope.student.id } -${requestScope.student.name } <br/>
</body>
</html>
处理器:
package com.test.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.test.entity.Student;
@Controller
@RequestMapping(value="handler")
public class SpringMVCHandler {
@RequestMapping(value="testModelAndView")
public ModelAndView testModelAndView() {
ModelAndView mv = new ModelAndView("success");//view: views/success.jsp
Student student = new Student() ;
student.setId(1);
student.setName("hhh");
mv.addObject("student", student);//相当于request.setAttribute("student", student);
return mv;//数据和页面都返回了
}
}
至此,ModelAndView的用法演示结束。
补充:
1.ModelMap 、Map、Model 的用法也非常简单,
示例:
public String testModel(ModellMap | Model |Map<String,Object> m) {
...
m.put(x,".."); 就会将x对象 放入request域中
...
}
2.使用@SessionAttributes(..)在类前标上注解可以将数据放入session中。
@SessionAttributes(value="student") //在request中存放student对象,同时将该对象放入session域中
@SessionAttributes(types= {Student.class,Address.class}) //在request中存放Student类型、Address类型的对象,同时将该类型对象放入session域中
3.@ModelAttribute的使用:
在任何一次请求前,都会先执行@ModelAttribute修饰的方法,该方法中的map.put()可以将对象放入即将查询的参数中,但必须满足约定:map.put(k,v) 中的k必须是即将查询的方法参数的首字母小写。