域对象共享数据

域对象共享数据

框架搭建

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceResponseEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:SpringMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

SpringMVC.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--开启组件扫描-->
    <context:component-scan base-package="com.louis.controller"></context:component-scan>
    <bean id="ThymeleafViewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <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>
</beans>

firstPage.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
    <title>首页</title>
</head>
<body>
首页
</body>
</html>

success.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
    <title>Title</title>
</head>
<body>
success
</body>
</html>

controller

@Controller
public class SetFirstPage {
    @RequestMapping("/")
    public String testFirstPage(){
        return "firstPage";
    }
}

在这里插入图片描述

域对象共享数据

1、使用servletAPI

firstPage.html
<a th:href="@{/testRequestByServletAPI}">通过servletAPI向request域对象共享数据</a>
ScopeControoler
@Controller
public class ScopeController {
    //使用servletAPI向request域对象共享数据
    @RequestMapping("/testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request){
        request.setAttribute("testRequestScope", "hello servletAPI");
        return "success";
    }
}
success.html
<!--操作标签中的文本内容,使用$-->
<p th:text="${testRequestScope}"></p>

在这里插入图片描述

在这里插入图片描述

2、使用SpringMVC提供的API

①ModelAndView
firstPage.html
<a th:href="@{/testModelAndView}">通过ModelAndView向request域对象共享数据</a>
ScopeController
@RequestMapping("/testModelAndView")
//使用ModelAndView返回值必须是ModelAndView
public ModelAndView testModelAndView(){
    ModelAndView mav = new ModelAndView();
    //处理模型数据,即向请求域request数据
    mav.addObject("testScope", "Hello ModelAndView");
    //设置视图名称,相当于平时返回success
    mav.setViewName("success");
    return mav;
}
success.html
<p th:text="${testScope}"></p>

在这里插入图片描述


②Model
firstPage.html
<a th:href="@{/testModel}">通过Model向request域对象共享数据</a>
ScopeController
@RequestMapping("/testModel")
public String testModel(Model model){
    model.addAttribute("testScope", "Hello model");
    return "success";
}

在这里插入图片描述


③map
firstPage.html
<a th:href="@{/testMap}">通过Map向request域对象共享数据</a><br/>
ScopeController
@RequestMapping("/testMap")
public String testMap(Map map){
   map.put("testScope", "Hello Map");
   return "success";
}

在这里插入图片描述


④ModelMap
firstPage.html
<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据</a><br/>
ScopeController
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
    modelMap.addAttribute("testScope", "Hello ModelMap");
    return "success";
}

在这里插入图片描述


Model、ModelMap、Map之间的关系

ModelModelMapMap类型的参数其实本质上都是BindingAwareModelMap类型

public interface Model{}
public class ModelMap extendes LinkedHasMap<String, Object>{}
public clsss ExtendedModelMap extends ModeMap implements Model{}
public class BindingAwarModelMap extends ExtendedModelMap{}

3、session域共享数据

firstPage.html
<a th:href="@{/testSession}">通过servletApi向Session域对象共享数据</a><br/>
ScopeController
@RequestMapping("/testSession")
public String testSession(HttpSession session){
    session.setAttribute("testSessionScope", "Hello session");
    return "success";
}
success.html
<!--在thymeleaf中获取session中数据-->
<p th:text="${session.testSessionScope}"></p>

在这里插入图片描述


4、application域共享数据

firstPage.html
<a th:href="@{/testApplication}">通过servletApi向Application域对象共享数据</a><br/>
ScopeController
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
    ServletContext application = session.getServletContext();
    application.setAttribute("testApplicationScope", "Hello Application");
    return "success";
}
success.html
<p th:text="${application.testApplicationScope}"></p>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值