SpringMVC域对象共享数据
1、ServletAPI的setAttribute()方法
<a th:href="@{/share/servletAPI}">测试servletAPI共享数据</a><br>
@RequestMapping("/share/servletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("api","ServletAPI共享数据");
return "success";
}
<p th:text="${api}"></p><br>
2、ModelAndView向request(请求)域对象共享数据
<a th:href="@{/share/mav}">测试ModelAndView共享数据</a><br>
@RequestMapping("/share/mav")
public ModelAndView testMAV(){
ModelAndView mav = new ModelAndView();
mav.addObject("mav","ModelAndView共享数据");
mav.setViewName("success");
return mav;
}
<p th:text="${mav}"></p>
3、Model向request(请求)域对象共享数据(常用)
<a th:href="@{/share/model}">测试Model共享数据</a><br>
@RequestMapping("/share/model")
public String testModel(Model model){
model.addAttribute("testModel","Model共享数据");
return "success";
}
<p th:text="${testModel}"></p>
4、ModelMap向request(请求)域对象共享数据
<a th:href="@{/share/modelMap}">测试modelMap共享数据</a><br>
@RequestMapping("/share/modelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testModelMap","ModelMap共享数据");
return "success";
}
<p th:text="${testModelMap}"></p>
5、Map向request(请求)域对象共享数据
<a th:href="@{/share/map}">测试Map共享数据</a><br>
@RequestMapping("/share/map")
public String testMap(Map<String,Object> map){
map.put("testMap","Map共享数据");
return "success";
}
<p th:text="${testMap}"></p>
6、向session(会话)域共享数据
<a th:href="@{/share/session}">测试session会话共享数据</a><br>
@RequestMapping("/share/session")
public String testSession(HttpSession session){
session.setAttribute("testSession","Session共享数据");
return "success";
}
<p th:text="${session.testSession}"></p>
7、向application(应用)域共享数据
<a th:href="@{/share/application}">测试application应用共享数据</a><br>
@RequestMapping("/share/application")
public String testApplication(HttpSession session){
ServletContext context = session.getServletContext();
context.setAttribute("testApplication","Application共享数据");
return "success";
}
<p th:text="${application.testApplication}"></p>