目录
2、使用ModelAndView向request域对象共享数据
1、使用ServletAPI向request域对象共享数据
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@Controller
public class ScopeController {
//使用ServletAPI向request域对象共享数据
@RequestMapping("/testRequestByServletAPI")
public String testRequestByServletAPI(HttpServletRequest request) {
request.setAttribute("testRequestScope", "hello,servletAPI");//往域对象中共享数据 s:键 o:值
// request.getAttribute("testRequestScope", "hello,servletAPI")//获取共享数据
// request.removeAttribute("testRequestScope", "hello,servletAPI");//删除共享数据
return "success";//转发
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
成功 success<br>
<p th:text="${testRequestScope}"></p>
</body>
</html>
2、使用ModelAndView向request域对象共享数据
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
ModelAndView mav = new ModelAndView();
//处理模型数据,即向请求域request共享数据
mav.addObject("testModelAndView","hello,ModelAndView");
//设置视图名称
mav.setViewName("success");
return mav;
}
<a th:href="@{/testModelAndView}">使用ModelAndView向request域对象共享数据</a>
3、使用Model向request域对象共享数据
@RequestMapping("/testModel")
public String testModel(Model model) {
//处理模型数据,即向请求域request共享数据
model.addAttribute("testModelAndView", "hello,Model");
return "success";
}
<br>
<a th:href="@{/testModel}">使用Model向request域对象共享数据</a>
4、使用map向request域对象共享数据
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("testRequestScope","hello,map");
return "success";
}
<br>
<a th:href="@{/testMap}">4、使用map向request域对象共享数据</a>
5、使用ModelMap向request域对象共享数据
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testRequestScope","hello,modelmap");
return "success";
}
6、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 {}
7、向session域共享数据
ScopeController类
@RequestMapping("/testSession")
public String testSession(HttpSession httpSession){
httpSession.setAttribute("testSessionScope","hello,session");
return "success";
}
index.xml
<br>
<a th:href="@{/testSession}">7、通过ServletAPI向向session域共享数据</a>
success.xml
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
成功 success<br>
<p th:text="${testRequestScope}"></p>
<!--通过ServletAPI向向session域共享数据采用此方法-->
<p th:text="${session.testSessionScope}"></p>
</body>
</html>
8、向application域共享数据
@RequestMapping("/testApplication")
public String testApplication(HttpSession httpSession){
ServletContext application=httpSession.getServletContext();
application.setAttribute("testApplicationScope","hello,application");
return "success";
}
success.xml
<p th:text="${application.testApplicationScope}"></p>