域对象:

  1. Request: 一次请求中的数据
  2. Session: 浏览器开始到浏览器关闭,和服务器是否关闭无关(钝化:浏览器没有关闭,服务器关闭序列化到磁盘; 和活化: 从钝化的数据中读取)
  3. Application(Servlet Context): 服务器开始到服务器关闭,和浏览器关闭无关

    示例

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <filter>
    7. <filter-name>CharacterEncodingFilter</filter-name>
    8. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    9. <init-param>
    10. <param-name>encoding</param-name>
    11. <param-value>UTF-8</param-value>
    12. </init-param>
    13. <init-param>
    14. <param-name>forceResponseEncoding</param-name>
    15. <param-value>true</param-value>
    16. </init-param>
    17. </filter>
    18. <filter-mapping>
    19. <filter-name>CharacterEncodingFilter</filter-name>
    20. <url-pattern>/*</url-pattern>
    21. </filter-mapping>
    22. <servlet>
    23. <servlet-name>DispatcherServlet</servlet-name>
    24. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    25. <init-param>
    26. <param-name>contextConfigLocation</param-name>
    27. <param-value>classpath:springMVC.xml</param-value>
    28. </init-param>
    29. <load-on-startup>1</load-on-startup>
    30. </servlet>
    31. <servlet-mapping>
    32. <servlet-name>DispatcherServlet</servlet-name>
    33. <url-pattern>/</url-pattern>
    34. </servlet-mapping>
    35. </web-app>
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6. <context:component-scan base-package="com.hqw.mvc"></context:component-scan>
    7. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    8. <property name="order" value="1"/>
    9. <property name="characterEncoding" value="UTF-8"/>
    10. <property name="templateEngine">
    11. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
    12. <property name="templateResolver">
    13. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    14. <!-- 视图前缀 -->
    15. <property name="prefix" value="/WEB-INF/templates/"/>
    16. <!-- 视图后缀 -->
    17. <property name="suffix" value=".html"/>
    18. <property name="templateMode" value="HTML5"/>
    19. <property name="characterEncoding" value="UTF-8" />
    20. </bean>
    21. </property>
    22. </bean>
    23. </property>
    24. </bean>
    25. </beans>
    1. <!DOCTYPE html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页</title>
    6. </head>
    7. <body>
    8. <a th:href="@{/testServletAPI}">testRequestByServletAPI</a><br>
    9. <a th:href="@{/testModelAndView}">testRequestByMAV</a><br>
    10. <a th:href="@{/testModel}">testRequestByModel</a><br>
    11. <a th:href="@{/testMap}">testMap</a><br>
    12. <a th:href="@{/testModelMap}">testModelMap</a>
    13. </body>
    14. </html>
    1. <!DOCTYPE html>
    2. <html lang="en" ç>
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <h1>成功</h1>
    9. <p th:text="${testRequestScope}">通过request共享数据</p>
    10. </body>
    11. </html>

    Request域对象

    使用ServletAPI向request域对象共享数据

    1. @Controller
    2. public class TestController {
    3. @RequestMapping("/")
    4. public String index() {
    5. return "index";
    6. }
    7. @RequestMapping("/testServletAPI")
    8. public String testServletAPI(HttpServletRequest httpServletRequest) {
    9. httpServletRequest.setAttribute("testRequestScope", "hello,servlet");
    10. return "success";
    11. }
    12. }

    使用ModelAndView向request域对象共享数据(建议)

    1. @RequestMapping("/testModelAndView")
    2. public ModelAndView testModelAndView() {
    3. /**
    4. * ModelAndView有Model和View的功能
    5. * Model主要用于向请求域共享数据
    6. * View主要用于设置视图,实现页面跳转
    7. */
    8. ModelAndView mav = new ModelAndView();
    9. //处理模型数据,即向请求域request共享数据
    10. mav.addObject("testRequestScope", "hello,Servlet");
    11. //设置视图名称,相当于返回字符串
    12. mav.setViewName("success");
    13. return mav;
    14. }

    使用Model向request域对象共享数据

    1. @RequestMapping("/testModel")
    2. public String testModel(Model model) {
    3. model.addAttribute("testRequestScope", "hello,model");
    4. return "success";
    5. }

    使用map向request域对象共享数据

    1. @RequestMapping("/testMap")
    2. public String testMap(Map<String, Object> map){
    3. map.put("testScope", "hello,Map");
    4. return "success";
    5. }

    用ModelMap向request域对象共享数据

    1. @RequestMapping("/testModelMap")
    2. public String testModelMap(ModelMap modelMap){
    3. modelMap.addAttribute("testRequestScope", "hello,ModelMap");
    4. return "success";
    5. }

    Model、ModelMap、Map的关系

    Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的

    1. public interface Model{}
    2. public class ModelMap extends LinkedHashMap<String, Object> {}
    3. public class ExtendedModelMap extends ModelMap implements Model {}
    4. public class BindingAwareModelMap extends ExtendedModelMap {}

    无论使用上面的哪种方法,最后在**DispatcherServlet**中都会被封装为**ModelAndView**

    session域共享数据

    1. @RequestMapping("/testSession")
    2. public String testSession(HttpSession session){
    3. session.setAttribute("testSessionScope", "hello,session");
    4. return "success";
    5. }
    1. <!DOCTYPE html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <h1>成功</h1>
    9. <p th:text="${testRequestScope}"></p>
    10. <p th:text="${session.testSessionScope}"></p>
    11. </body>
    12. </html>

    向application域共享数据

    1. @RequestMapping("/testApplication")
    2. public String testApplication(HttpSession session){
    3. ServletContext application = session.getServletContext();
    4. application.setAttribute("testApplicationScope", "hello,application");
    5. return "success";
    6. }

    application.testApplicationScope