一、ModelAndView
1、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="index/requestModelAndView" method="post">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="text" name="stucard.cardId" />
<input type="text" name="stucard.cardName" />
<input type="submit" value="带返回值 ModelAndView"/>
</form>
</body>
</html>
2、控制器类
通过 ModelAndView 的构造函数指定转发页面(可以由视图解析器添加前后缀),并通过 ModelAndView 的 addobject(String attributeName, Object attributeValue)方法设置返回值
/**
* Servlet implementation class Test
*/
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
/**
* 带返回值--ModelAndView
* @param student
* @return
*/
@RequestMapping(value={"/requestModelAndView"})
public ModelAndView requestModelAndView(Student student) {
//将 welcome.jsp放在构造函数中
ModelAndView modelAndView=new ModelAndView("welcome");
//将返回值放入request域中
modelAndView.addobject("studentMav",student);
return modelAndView;
}
}
3、跳转页面 : welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
返回值:</br>
ModelAndView :</br>
name:${requestScope.studentMav.name} </br>
cardId:${requestScope.studentMav.stucard.cardId}</br>
cardName:${requestScope.studentMav.stucard.cardName}</br>
</body>
</html>
二、ModelMap
1、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="index/requestModelMap" method="post">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="text" name="stucard.cardId" />
<input type="text" name="stucard.cardName" />
<input type="submit" value="带返回值 ModelMap"/>
</form>
</body>
</html>
2、控制器类
/**
* Servlet implementation class Test
*/
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
/**
* 带返回值--ModelMap
* @param student
* @param modelMap
* @return
*/
@RequestMapping(value={"/requestModelMap"})
public String requestModelMap(Student student,ModelMap modelMap) {
modelMap.put("studentMm", student);
return "welcome";
}
}
3、跳转页面 : welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
ModelMap :</br>
name:${requestScope.studentMm.name} </br>
cardId:${requestScope.studentMm.stucard.cardId}</br>
cardName:${requestScope.studentMm.stucard.cardName}</br>
</body>
</html>
三、Model
1、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="index/requestModel" method="post">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="text" name="stucard.cardId" />
<input type="text" name="stucard.cardName" />
<input type="submit" value="带返回值 Model"/>
</form>
</body>
</html>
2、控制器类
/**
* Servlet implementation class Test
*/
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
/**
* 带返回值--Model
* @param student
* @param model
* @return
*/
@RequestMapping(value={"/requestModel"})
public String requestModel(Student student,Model model) {
model.addAttribute("studentModel", student);
return "welcome";
}
}
3、跳转页面 : welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Model :request</br>
name:${requestScope.studentModel.name} </br>
cardId:${requestScope.studentModel.stucard.cardId}</br>
cardName:${requestScope.studentModel.stucard.cardName}</br></br></br>
</body>
</html>
四、Map
1、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="index/requestMap" method="post">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="text" name="stucard.cardId" />
<input type="text" name="stucard.cardName" />
<input type="submit" value="带返回值 Map"/>
</form>
</body>
</html>
2、控制器类
/**
* Servlet implementation class Test
*/
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
/**
* 带返回值--Map
* @param student
* @param map
* @return
*/
@RequestMapping(value={"/requestMap"})
public String requestMap(Student student,Map<String,Object> map) {
map.put("studentMap", student);
return "welcome";
}
}
3、跳转页面 : welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Map :request</br>
name:${requestScope.studentMap.name} </br>
cardId:${requestScope.studentMap.stucard.cardId}</br>
cardName:${requestScope.studentMap.stucard.cardName}</br></br>
</body>
</html>
五、将返回信息放入session
@SessionAttributes(value ={“studentMap”,“studentModel”}):根据已在request与中设置的 key 设置 session
/**
* Servlet implementation class Test
*/
@SessionAttributes(value ={"studentMap","studentModel"})
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
/**
* 带返回值--Model
* @param student
* @param model
* @return
*/
@RequestMapping(value={"/requestModel"})
public String requestModel(Student student,Model model) {
model.addAttribute("studentModel", student);
return "welcome";
}
/**
* 带返回值--Map
* @param student
* @param map
* @return
*/
@RequestMapping(value={"/requestMap"})
public String requestMap(Student student,Map<String,Object> map) {
map.put("studentMap", student);
return "welcome";
}
}
六、@modelattribute
1、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="index/requestmodelattribute" method="post">
<input type="submit" value="@modelattribute"/>
</form>
</body>
</html>
2、控制器类
1、@modelattribute:在任何一次请求前,都会先执行 @modelattribute
注解的方法
2、@modelattribute(“studentAttr”):通过@modelattribute("studentAttr")
注解将 @modelattribute
注解 的方法的 map 返回值注入 @modelattribute("studentAttr")
所注解的参数
/**
* Servlet implementation class Test
*/
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
@modelattribute//在任何一次请求前,都会先执行 @modelattribute 注解 的方法
public void queryStudent(Map<String,Object> map) {
Student student =new Student();
student.setId("11");
student.setName("myname");
map.put("studentAttr", student);
System.out.println("queryStudent--------------------");
}
/**
* @modelattribute
* @param student
* @param map
* @return
*/
@RequestMapping(value={"/requestmodelattribute"})
//若省略@modelattribute("studentAttr"),则@modelattribute所注释的方法中Map的key值默认必须为该方法参数类型的首字母小写(而非变量名),即 student
public String requestRequestmodelattribute(@modelattribute("studentAttr") Student student,Map<String,Object> map) {
System.out.println("requestrequestmodelattribute--------------------");
map.put("studentModelAttr", student);
return "welcome";
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。