作用域对象request,session, servletContext中的数据在Thymeleaf中的显示都是相同的

作用域对象中的 List和Set的集合在html中的显示是相同的

作用域对象中的显示字符串或基本类型是相同的

springboot中怎样使用session?

方式一,使用注解@SessionAttributes确定键,然后用ModerAndView对象添加对应的值后返回页面

方式二 处理方法中使用参数HttpSession

springboot怎样获取ServletContext

方法一 request获取servletContext

ServletContext servletContext = request.getServletContext();

方法二 使用ContextLoader

ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();

方法三 使用spring注入自动注入

@Autowired private ServletContext servletContext;

在Thymeleaf中显示作用域对象字符串或基本类型

将要显示的对象设置在标签内部,就算标签内部有内容也会被替换掉, 也可以使用不存在的标签替换

设置方式:${name}

设置为标签属性的值,要在属性前面加上"th:"

 1 modelAndView.addObject("i", 100);
2 -----------------------------------------------------------------
3 <span th:text="${i}"></span>
4 <div th:text="${i}"></div>
5 <h1 th:text="${i}"></h1>
6 <wgr style="color: red;" th:text="${i}"></wgr>
7 <span style="color: red;" th:text="${i}"></span>
8 <div style="color: red;" th:text="${i}"></div>
9 <h1 style="color: red;" th:text="${i}"></h1>
10 <wgr style="color: red;" th:text="${i}"></wgr> <br />
11 <input type="text" th:value="${i}" />
12 <input type="submit" th:value="${i}"/>
13 <input type="text" th:id="${i}" />

对象

1 modelAndView.addObject("stu", new Stu(1, "小明", 12));
2
3 ---------------------------------------------------------
4
5 <span th:text="${stu.id}"> </span>
6
7 <span th:text="${stu.name}"> </span>
8
9 <span th:text="${stu.age}"> </span>

集合遍历

1 List<String> strList = new ArrayList<>();
2 strList.add("小明");
3 strList.add("小华");
4 strList.add("小陶");
5 modelAndView.addObject("strList", strList);
6 ---------------------------------------------------------
7 <div th:each="str:${strList}"> //div可以改为任意的容器标签,甚至是不存在的标签
8 <span style="color: red;" th:text="${str}"></span> <br />
9 </div>

显示session和ServletContext中数据

默认显示request中数据,

显示session中数据要加上 session前缀

显示servletContext中数据要加上application前缀

 1 //装到request
2 request.setAttribute("requestAge", 100);
3 //装到session
4 session.setAttribute("sessionName", "小明");
5 //装到ServletContext
6 servletContext.setAttribute("applicationNum", 1);
7 --------------------------------------------------------------------------------------
8 request中:
9 <span style="color: red;" th:text="${requestAge}"></span><br />
10 session中:
11 <span style="color: red;" th:text="${session.sessionName}"></span><br />
12 servletContext中:
13 <span style="color: red;" th:text="${application.applicationNum}"></span><br />

SpringBoot入门06-Thymeleaf显示作用域对象种的对象的更多相关文章

  1. SpringBoot入门篇--Thymeleaf引擎模板的基本使用方法

    我们在使用SpringBoot框架的时候在前面已经介绍了Thymelea引擎模板,因为SpringBoot对JSP惨不忍睹的支持.那我们在使用引擎模板对前端页面进行渲染能够返回的情况下我们怎么才能在静 ...

  2. SpringBoot入门之Thymeleaf的使用

    在.net的MVC3 或更高版本等支持 Razor 的框架里使用cshtml,Razor是一种简单的编程语法,用于在网页中嵌入服务器端代码.在使用springboot开发mvc时也有与.net类似的视 ...

  3. SpringBoot入门07-Thymeleaf中显示ajax请求到的数据

    Thymeleaf中显示ajax请求所需依赖 <!--所需依赖--><dependency> <groupId>org.springframework.boot&l ...

  4. SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)

    SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...

  5. SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)

    Spring Boot 提供了spring-boot-starter-web来为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及SpringMVC的 ...

  6. SpringBoot入门系列(五)Thymeleaf的常用标签和用法

    前面介绍了Spring Boot 中的整合Thymeleaf .不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/16577 ...

  7. springboot笔记06——使用Thymeleaf模板引擎

    前言 Springboot 推荐使用Thymeleaf做视图层.Thymeleaf支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式.浏览器解释 html 时会忽略 ...

  8. SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染

    在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...

  9. SpringBoot入门系列(四)整合模板引擎Thymeleaf

    前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...

随机推荐

  1. 使用junit进行最简单的单元测试

    使用junit进行最简单的单元测试 使用工具: jdk IDEA Maven 第一步 创建一个Maven项目 第二步 导入junit依赖 <dependency> <groupId& ...

  2. 【Spring 5.x】学习笔记汇总

    Spring 工厂 工厂设计模式.第一个Spring程序细节分析.整合日志框架 注入详解 - Set注入(JDK内置类型,用户自定义类型).构造注入(重载) 反转控制与依赖注入.Spring工厂创建复 ...

  3. 史上最全git命令集

    配置化命令 git config --global user.name "Your Name" git config --global user.email "email ...

  4. Python - 面向对象编程 - 三大特性之多态

    前置知识 封装 详解文章:https://www.cnblogs.com/poloyy/p/15203989.html 封装根据职责将属性.方法封装到一个抽象的类中 定义类的准则-封装 继承 详解文章 ...

  5. java版gRPC实战之一:用proto生成代码

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. MyBatis的Mapper代理笔记

    MaBatis--Mapper代理 目前使用SqlSession进行增删改查的缺点: 没有办法实现多参传值 书写的时候没有接口,后期的维护低 使用Mapper的动态代理方式来解决问题 具体实现 首先我 ...

  7. 乐肴2.0环境改名后,需要删除原来的软链接(public/storage)

    rm -rf public/storage php artisan storage:link

  8. 马哈鱼数据血缘分析器分析case-when语句

    马哈鱼数据血缘分析器是一个分析数据血缘关系的平台,可以在线直接递交 SQL 语句进行分析,也可以选择连接指定数据库获取 metadata.从本地上传文件目录.或从指定 git 仓库获取脚本进行分析. ...

  9. golang 注释 exported function xxx should have comment or be unexported

    0x00 问题 exported function xxx should have comment or be unexported. 0x01 解决 https://golang.org/s/sty ...

  10. 面试官:MySQL的幻读是怎么被解决的?

    大家好,我是小林. 我之前写过一篇数据库事务的文章「 事务.事务隔离级别和MVCC」,这篇我说过什么是幻读. 在这里插入图片描述 然后前几天有位读者跟我说,我这个幻读例子不是已经被「可重复读」隔离级别 ...