作用域对象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. VS Code 1.60 发布!竟然可以自动检测编程语言了!

    北京时间 2021 年 9 月 3 日凌晨,微软正式发布 2021 年 8 月版的 Visual Studio Code.希望您会喜欢此版本中的许多更新与改进,以下是其中的一些亮点: * 自动语言检测 ...

  2. Linux系统下的软件管理(rpm)、搭建第三方软件库、yum的黑名单

    对wps-office进行模糊匹配照样可以查找出该软件yum clean all ? ? ? ?##清空yum缓存识别新配置 测验安装wps软件: 安装成功即可使用办公软件 1.yum install ...

  3. Springboot_Email注解爆红

    应该是更新后的版本,不会自动导入pom依赖 <!--新版本需要validation启动器 --> <dependency> <groupId>org.springf ...

  4. 关于 antd tree 组件的推拽操作

    最近项目中使用到 tree 组件的推拽操作, 按常理来说应该主要用到其中的 onDrop 事件,但其中的参数又没有详细的说明,只是在官网给了个例子,网上搜索后又没有发现到位的总结. 因此经过N次的测试 ...

  5. JQ动画

    /* //基本 show([s,[e],[fn]]) 显示元素 hide([s,[e],[fn]]) 隐藏元素 //滑动 slideDown([s],[e],[fn]) 向下滑动 slideUp([s ...

  6. Java并发之Synchronized机制详解

    带着问题阅读 1.Synchronized如何使用,加锁的粒度分别是什么 2.Synchronized的实现机制是什么 3.Synchronized是公平锁吗 4.Java对Synchronized做 ...

  7. 安全系列之:跨域资源共享CORS

    目录 简介 CORS举例 CORS protocol HTTP request headers HTTP response headers 基本CORS Preflighted requests 带认 ...

  8. SpringSecurity-图解

  9. c# 扩展方法奇思妙用基础篇九:Expression 扩展

    http://www.cnblogs.com/ldp615/archive/2011/09/15/expression-extension-methods.html .net 中创建 Expressi ...

  10. CDI 组件拦截器的使用和学习

    拦截器的作用原理: 声明拦截器,加@Interceptor注解 方法有二: 1)为拦截器添加Qualifier: 2)不添加Qualifier.为拦截器添加具体的拦截方法,该方法加@AroundInv ...