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/ ...
随机推荐
- Spring Boot +Vue 项目实战笔记(二):前后端结合测试(登录页面开发)
前言:关于开发环境 每位 Coder 都有自己偏好的开发工具,从大的方面划分主要有文本编辑器流和 IDE 流两种,我有一段时间也喜欢用编辑器(Sublime Text.Vim),但对我来说开发效率确实 ...
- JavaWeb使用Filter进行字符编码过滤 预防web服务中文乱码
JavaWeb使用Filter进行字符编码过滤 预防web服务中文乱码 准备条件:一个创建好的 JavaWeb 项目 步骤: 1.创建一个类并实现 Filter 接口 import javax.ser ...
- JDBC分页查询及实现
当数据过多时,一页之内是无法显示的,因此需要进行分页显示. (一)分页技术实现: 物理分页: - 在数据库执行查询时(实现分页查询),查询需要的数据--依赖数据库的SQL语句 - 在sql查询时,从数 ...
- php open_basedir绕过
描述 php为了安全性考虑,有一项 open_basedir 的设置,它可将用户访问文件的活动范围限制在指定的区域.根据你web服务器环境,open_basedir可以在几个地方设置. 首先 在php ...
- CommonsBeanutils1 分析笔记
1.PropertyUtils.getProperty commons-beanutils-1.9.2.jar 包下的 PropertyUtils#getProperty方法相对于getXxx方法,取 ...
- 缩减Centos7xfs磁盘空间
问题描述:df -h查看 root目录仅有20G空间,其余300G空间全在home目录下.xfs不可以直接缩减,所以只能删除xfs盘然后重新添加. 解决办法: 1. 注释想要删除的磁盘,此处以cent ...
- Ubuntu管理员手册
hostname cat /etc/hosts apt-get install iproute2 iproute2-doc ip a ps -aux
- go相关
mac 上build go 如果想要在centos上面执行 必须使用下面的方式 CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o hello ...
- vue-cli 项目中使用 v-chart 及导出 chart 图片
安装: npm i v-charts echarts -S 组件中使用: 1 <template> 2 <div class="app-chart"> 3 ...
- C++ 输入一行未知个数的整数
C++ 输入一行未知个数的整数 代码: #include<iostream> #include<vector> using namespace std; int main() ...