1.前端编码
index.xml
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello world!</h1>
<a th:href="@{/testServletAPI}">1.使用ServletAPI向request域对象共享数据</a><br>
<a th:href="@{/testModelAndView}">2.使用ModelAndView向request域对象共享数据</a><br>
<a th:href="@{/testModel}">3.使用Model向request域对象共享数据</a><br>
<a th:href="@{/testMap}">4.使用Map向request域对象共享数据</a><br>
<a th:href="@{/testModelMap}">5.使用ModelMap向request域对象共享数据</a><br>
<a th:href="@{/testSession}">6.使用ServletAPI向Session域共享数据</a><br>
<a th:href="@{/testApplication}">7.使用ServletAPI向application域共享数据</a><br>
</body>
</html>
success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>success!</h1>
<p th:text="${testScope}"></p>
<p th:text="${session.testScope}"></p>
<p th:text="${application.testScope}"></p>
</body>
</html>
2.域对象共享数据方法
1.使用ServletAPI向request域对象共享数据
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}
2.使用ModelAndView向request域对象共享数据
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
/**
* ModelAndView有Model和View的功能
* Model主要用于向请求域共享数据
* View主要用于设置视图,实现页面跳转
*/
ModelAndView mv =new ModelAndView();
//向请求域共享数据
mv.addObject("testScope", "hello,ModelAndView");
//设置视图,实现页面跳转
mv.setViewName("success");
return mv;
}
3.使用Model向request域对象共享数据
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
4.使用Map向request域对象共享数据
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}
5.使用ModelMap向request域对象共享数据
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}
6.使用ServletAPI向Session域共享数据
@RequestMapping("/testSession")
public String testHttpSession(HttpSession httpSession){
httpSession.setAttribute("testScope", "hello,Session");
return "success";
}
7.使用ServletAPI向application域共享数据
@RequestMapping("/testApplication")
public String testApplication(HttpSession httpSession){
ServletContext application = httpSession.getServletContext();
application.setAttribute("testScope", "hello,Application");
return "success";
}
3.后端编码
package com.limi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class LoginController {
@RequestMapping("/index")
public String test(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
/**
* ModelAndView有Model和View的功能
* Model主要用于向请求域共享数据
* View主要用于设置视图,实现页面跳转
*/
ModelAndView mv =new ModelAndView();
//向请求域共享数据
mv.addObject("testScope", "hello,ModelAndView");
//设置视图,实现页面跳转
mv.setViewName("success");
return mv;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}
@RequestMapping("/testSession")
public String testHttpSession(HttpSession httpSession){
httpSession.setAttribute("testScope", "hello,Session");
return "success";
}
@RequestMapping("/testApplication")
public String testApplication(HttpSession httpSession){
ServletContext application = httpSession.getServletContext();
application.setAttribute("testScope", "hello,Application");
return "success";
}
}
4.小知识
1.Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的, 就是这三个最后都会被BindingAwareModelMap所继承或实现, 从而实例化成BindingAwareModelMap 类型.
public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
2.session中的数据默认会保存30分钟, 若30分钟该数据仍然未被访问, 则会清除.
3.session一般可以用来保存登录信息.