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 /> 
复制代码

 

posted @   四叶笔记  阅读(4264)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示