SpringMVC(四)域对象共享数据(干货、超详细)

【环境说明】
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共享的一个地方吧

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值