【环境说明】
SpringMVC项目中使用的是thymeleaf视图解析器
<!-- Spring和thymeleaf的整合-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
<!--不能用3.1 3.1是和Spring6结合的 可以看官网文档https://www.thymeleaf.org/doc/tutorials/3.1/thymeleafspring.html#integrating-thymeleaf-with-spring-->
</dependency>
SpringMVC.xml的位置文件
<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<!-- 视图解析器的顺序,可以看出视图解析器可以配置多个 -->
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
1、使用ServletAPI向request域对象共享数据
pom.xml中添加servlet-api依赖,提供了Servlet的一些API接口
<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<scope>provided</scope>
servlet-api只能作用在编译和测试时,因为在Tomcat容器中有servlet-api依赖,不用将这个依赖打包。
在Controller中建立一个控制器,调用ServletAPI,request request.setAttribute(String ,Object);向request域写数据,域中的数据也是键值对存在,在html中利用Thymeleaf语法将域中的数据取出来。
@RequestMapping("/hello")
public String hello(HttpServletRequest request){
request.setAttribute("requestScope", "hello,servletAPI");
return "hello";
}
在hello.html中设置
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${requestScope}"></p>
</body>
</html>
${}
是Thymleaf的语法,取出requestScope的值
运行结果:
2 使用ModelAndView向request域对象共享数据
@RequestMapping("/hello")
public ModelAndView hello(){
ModelAndView mav = new ModelAndView();
//向请求域共享数据
mav.addObject("requestScope", "hello,ModelAndView");
//设置视图,实现页面跳转
mav.setViewName("hello");
return mav;
}
ModelAndView有Model和View的功能
Model主要用于向请求域共享数据
View主要用于设置视图,实现页面跳转
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${requestScope}"></p>
</body>
</html>
3. 使用Model向request域对象共享数据
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("requestScope", "hello,Model");
return "hello";
}
正常返回String就行,model的值会带出去
4.使用map向request域对象共享数据
@RequestMapping("/hello")
public String hello(Map<String, Object> map){
map.put("requestScope", "hello,Map");
return "hello";
}
5 使用ModelMap向request域对象共享数据
@RequestMapping("/hello")
public String hello(ModelMap modelMap){
modelMap.addAttribute("requestScope", "hello,ModelMap");
return "hello";
}
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域共享数据
@RequestMapping("/hello")
public String hello(HttpSession session){
session.setAttribute("sessionScope", "hello,session");
return "hello";
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${session.sessionScope}"></p>
</body>
</html>
注意在session域中取值,用的是
session
点出来
8、向application域共享数据
@RequestMapping("/hello")
public String hello(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("applicationScope", "hello,application");
return "hello";
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${application.applicationScope}"></p>
</body>
</html>
在application域中个取值 用
application
点出来
猜测:为什么application叫ServletContext servlet上下文,好多个Servlet的上下文,就是好多个Servlet共享的一个地方吧