SpringBoot入门06-Thymeleaf显示作用域对象种的对象
作用域对象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显示作用域对象种的对象的更多相关文章
- SpringBoot入门篇--Thymeleaf引擎模板的基本使用方法
我们在使用SpringBoot框架的时候在前面已经介绍了Thymelea引擎模板,因为SpringBoot对JSP惨不忍睹的支持.那我们在使用引擎模板对前端页面进行渲染能够返回的情况下我们怎么才能在静 ...
- SpringBoot入门之Thymeleaf的使用
在.net的MVC3 或更高版本等支持 Razor 的框架里使用cshtml,Razor是一种简单的编程语法,用于在网页中嵌入服务器端代码.在使用springboot开发mvc时也有与.net类似的视 ...
- SpringBoot入门07-Thymeleaf中显示ajax请求到的数据
Thymeleaf中显示ajax请求所需依赖 <!--所需依赖--><dependency> <groupId>org.springframework.boot&l ...
- SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)
SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...
- SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)
Spring Boot 提供了spring-boot-starter-web来为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及SpringMVC的 ...
- SpringBoot入门系列(五)Thymeleaf的常用标签和用法
前面介绍了Spring Boot 中的整合Thymeleaf .不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/16577 ...
- springboot笔记06——使用Thymeleaf模板引擎
前言 Springboot 推荐使用Thymeleaf做视图层.Thymeleaf支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式.浏览器解释 html 时会忽略 ...
- SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染
在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...
- SpringBoot入门系列(四)整合模板引擎Thymeleaf
前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...
随机推荐
- Linux nginx 负载的几种方式
2021-08-191. 轮询 (这是默认的方式)就是在 nginx 映射的几个服务器按请求的时间顺序逐一分配,几率是随机的.如果后端服务器 down 掉,能自动忽略不用.这种情况一般是每台服务器配置 ...
- 使用 IDEA 配合 Dockerfile 部署 SpringBoot 工程
准备 SpringBoot 工程 新建 SpringBoot 项目,默认的端口是 8080 ,新建 Controller 和 Mapping @RestController public class ...
- 超详细kafka教程来啦
Kafka的概念和入门 Kafka是一个消息系统.由LinkedIn于2011年设计开发. Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度O(1)的方式提供消息持 ...
- ELK 收集 Tomcat日志以及修改Tomcat日志格式
ELK 收集 Tomcat日志以及修改Tomcat日志格式 Tomcat日志 想要收集tomcat 日志 首先我们要对tomcat的日志有足够的了解 tomca日志分类 简单的说tomcat logs ...
- hyperf从零开始构建微服务(一)——构建服务提供者
阅读目录 什么是服务 构建服务提供者 1.创建数据表 2.构建服务提供者 3.安装json rpc依赖 4.安装rpc server组件 5.修改server配置 6.配置数据库 7.编写基础代码 7 ...
- openwrt开发笔记三:uci移植及API调用
1.uci编译安装.移植 安装依赖 libubox #安装cmake sudo apt-get install cmake #下载依赖库libubox git clone http://git.nbd ...
- Appium自动化(11) - 详解 Applications 类里的方法和源码解析
如果你还想从头学起Appium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1693896.html 前言 Applications 类 ...
- [第十三篇]——Docker Compose之Spring Cloud直播商城 b2b2c电子商务技术总结
Docker Compose Compose 简介 Compose 是用于定义和运行多容器 Docker 应用程序的工具.通过 Compose,您可以使用 YML 文件来配置应用程序需要的所有服务.然 ...
- 珠峰2016,第9期 vue.js 笔记部份
在珠峰参加培训好年了,笔记原是记在本子上,现在也经不需要看了,搬家不想带上书和本了,所以把笔记整理下,存在博客中,也顺便复习一下 安装vue.js 因为方便打包和环境依赖,所以建意npm init ...
- sql语句异常向数据库插入数据报错
在php编程向数据库插入数据时报如下错误: [Err] 1064 - You have an error in your SQL syntax; check the manual that corre ...