文章目录
一、通过原生servletAPI向request域中共享数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
servletapi:<p th:text="${name}"></p><br/>
</body>
</html>
@RequestMapping("testRequestByServletAPI")
public String testRequestByServletAPI(HttpServletRequest request) {
request.setAttribute("name", "job");
return "success";
}
二、使用ModelAndView向request域对象共享数据
不管用的是什么方法,最后都会封装成ModelAndView对象。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
servletapi:<p th:text="${name}"></p><br/>
</body>
</html>
@RequestMapping("testRequestByModelAndView")
public ModelAndView testRequestByModelAndView() {
ModelAndView mav = new ModelAndView();
//向域对象中共享数据
mav.addObject("name", "modelAmdView");
//设置视图名称
mav.setViewName("success");
return mav;
}
三、使用Model向request域对象共享数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
servletapi:<p th:text="${name}"></p><br/>
</body>
</html>
@RequestMapping("testRequestByModel")
public String testRequestByModel(Model model) {
model.addAttribute("name", "model");
return "success";
}
四、使用map向request域对象共享数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
servletapi:<p th:text="${name}"></p><br/>
</body>
</html>
@RequestMapping("testRequestByMap")
public String testRequestByMap(Map<String,Object> map) {
map.put("name","map");
return "success";
}
五、使用modelMap向request域对象共享数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
servletapi:<p th:text="${name}"></p><br/>
</body>
</html>
@RequestMapping("testRequestByModelMap")
public String testRequestByModelMap(ModelMap modelMap) {
modelMap.addAttribute("name", "modelMap");
return "success";
}
六、Model、ModelMap与Map之间的关系
Model、ModelMap、Map类型的参数其实本质上都是BindingAwareModelMap类型的。
public interface Model {}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
七、向session域中共享数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
request:<p th:text="${name}"></p><br/>
session:<p th:text="${session.name}"></p><br/>
</body>
</html>
@RequestMapping("tesSessionByServletApi")
public String tesSessionByServletApi(HttpSession session){
session.setAttribute("name","session");
return "success";
}
八、向application域共享数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--request域中的数据可以直接写key,别的需要带前缀-->
<h1>success</h1><br/>
request:<p th:text="${name}"></p><br/>
session:<p th:text="${session.name}"></p><br/>
application:<p th:text="${application.name}"></p><br/>
</body>
</html>
@RequestMapping("testApplicationByServletApi")
public String testApplicationByServletApi(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("name","application");
return "success";
}