如果在前台, 我需要获取session中的信息, 或者需要获取url中的参数信息, 是不是需要在后台手动处理好, 然后放到Model中去, 在前台通过${}来取呢?

当然, 这种方式, 是可以的, 但是比较麻烦, 而且, 别人已经考虑到这个了, 我们直接用就可以了.

一. 基本对象

#ctx 上下文对象
#vars 上下文对象(和#ctx相同, 但是一般用#ctx)
#locale 上下文区域设置
#request (仅在Web Contexts中) HttpServletRequest对象
#response (仅在Web Contexts中) HttpServletResponse对象
#session (仅在Web Contexts中) HttpSession对象
#servletContext (仅在Web Contexts中) ServletContext对象

1. 数据准备

package org.elvin.learn.springboot.controller;

import org.elvin.learn.springboot.pojo.Book;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.MapUtils; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*; @Controller
@RequestMapping("thy")
public class ThyController { @Autowired
private HttpServletResponse response; @Autowired
private HttpServletRequest request; @GetMapping("index")
public String index(Model model) { Book book = new Book("springmvc", new DateTime().toString("yyyy-MM-dd"), 10000L);
model.addAttribute("book", book); HttpSession session = request.getSession();
session.setAttribute("session1", "lalala"); return "thy/index";
}
}

2. request / param

<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">reqeust</h3>
</div>
<div class="panel-body">
<p th:text="${#request.getContextPath()}"></p>
<div th:each="item : ${#request.getParameterNames()}">
<p th:text="|name : ${item}, value : ${#request.getParameter(item)}|"></p>
</div>
<hr /> <p th:text="|size : ${param.size()}, lang: ${param.lang}, arr: ${param.arr}, arr-first: ${param.arr[1]}|"></p>
</div>
</div>

#request -> HttpServletRequest 对象.

param : 用来获取请求参数的.

param可以通过直接点的方式, 来访问参数. 是非常方便的

除了size()方法, 还有 isEmpty() , containsKey('...')两个常用方法

2. session

html:

<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">session</h3>
</div>
<div class="panel-body">
<p th:text="${#session.getAttribute('session1')}"></p>
<p th:text="|size : ${session.size()} , value : ${session.session1} |"></p> <div th:each="item : ${#session.getAttributeNames()}">
<p th:text="|name : ${item}, value : ${#session.getAttribute(item)}|"></p>
</div>
</div>
</div>

结果:

#session -> HttpSession对象

session和param一样, 都可以通过点的方式来获取session, 除了size()方法, 还有 isEmpty() , containsKey('...')两个常用方法

3. 上下文对象 #ctx

ctx主要看 IContext 接口.

package org.thymeleaf.context;

import java.util.Locale;
import java.util.Set; public interface IContext {
Locale getLocale(); boolean containsVariable(String var1); Set<String> getVariableNames(); Object getVariable(String var1);
}

在后台写入Model的内容, 在前台都可以通过#ctx来获取

<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">上下文对象</h3>
</div>
<div class="panel-body"> <p th:text="${#ctx.getVariable('book').name}"></p> </div>
</div>

在后台传入一个集合或者一个字符串, 在前台使用时, 如果不知道集合,字符串是否为空, 是不是会导致一些问题呢?

那如果会导致问题, 那么在前台是否有方法来解决这些问题呢?

这里需要借助一些工具类, 来辅助判断.

二. 工具类对象

#execInfo 有关正在处理的模板的信息
#messages 用于在变量表达式中获取外部化消息的方法, 与使用#{...}语法获得的方式相同
#uris 转义 URL / URI 部分的方法
#conversions 执行配置的转换服务(如果有的话)的方法
#dates java.util.Date对象的方法: 格式化, 组件提取等
#calendars java.util.Calendar对象, 类似于#dates
#numbers 用于格式化数字对象的方法
#strings String对象的方法: contains, startsWith, prepending, appending等
#objects 一般对象的方法
#bools 布尔评估的方法
#arrays 数组的方法
#lists 列表的方法
#sets 集合的方法
#maps map方法
#aggregates 在数组或集合上创建聚合的方法
#ids 处理可能重复的id属性的方法

具体的使用方法, 可以通过 ctrl + 鼠标左键点击  的方式, 进类里面查看.

spring boot 与 thymeleaf (4): 基本对象、工具类对象的更多相关文章

  1. Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  2. 极简 Spring Boot 整合 Thymeleaf 页面模板

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  3. 一个小demo熟悉Spring Boot 和 thymeleaf 的基本使用

    目录 介绍 零.项目素材 一. 创建 Spring Boot 项目 二.定制首页 1.修改 pom.xml 2.引入相应的本地 css.js 文件 3.编辑 login.html 4.处理对 logi ...

  4. Spring Boot整合Thymeleaf模板引擎

    什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...

  5. 兼容 Spring Boot 1.x 和 2.x 配置类参数绑定的工具类 SpringBootBindUtil

    为了让我提供的通用 Mapper 的 boot-starter 同时兼容 Spring Boot 1.x 和 2.x,增加了这么一个工具类. 在 Spring Boot 中,能够直接注入 XXProp ...

  6. Spring Boot整合 Thymeleaf 模板引擎

    什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...

  7. Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例

    Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...

  8. spring boot 与 thymeleaf (2): 常用表达式

    在asp.net mvc 中, 有一个视图解析器, 可以支持Razor语法. 使用起来, 是非常的方便, 并且, 写在前台页面的后台方法, 是可调试的. 但是在java中, 目前我还没有接触到, 像. ...

  9. Spring Boot 2 + Thymeleaf:表单字段绑定、表单提交处理

    Spring Boot中Thymeleaf对表单处理的一些用法:(1)使用th:field属性:进行表单字段绑定(2)使用ids对象:一般用于lable配合radio或checkbox使用(3)表单提 ...

随机推荐

  1. (最长公共子序列 暴力) Common Subsequence (poj 1458)

    http://poj.org/problem?id=1458 Description A subsequence of a given sequence is the given sequence w ...

  2. 给公司服务器装web服务器,邮件服务器——安装SecureCRT

    系统用centos5.9 首先在window上安装SecureCRT终端 1:首先验证安装secureCRT的本地机和linux服务器能否ping的通: 2:判断linux 服务端是否安装了ssh 若 ...

  3. 23种设计模式(1)-Facade设计模式

    前记 曾经我遇见的一个需求是这样的,接口A有个方法void methodA(),类B需要实现接口A的methodA()方法,并且在类B中需要把methodA()方法内部处理逻辑获得的结果利用C类实例的 ...

  4. 第82讲:Scala中List的ListBuffer是如何实现高效的遍历计算的?

    今天学习下list中的ListBuffer实现的高效计算.让我们先来看下代码 def main(args:Array[String]){        val list = List(1,2,3,4, ...

  5. QT5.4.0安装以及与VS2010整合安装---64bit操作系统解决方案

    QT5.4.0安装以及与VS2010整合安装---64bit操作系统解决方案 注意,目前QT官网不能下载,必须提供注册,然后才可以下载. 网上不同版本安装的细节有差异,特将我的安装相关操作贴出来,希望 ...

  6. Eclipse运行wordcount步骤

    Eclipse运行wordcount步骤 第一步:建立工程,导入代码. 第二步:建立文件写入数据(以空格分开),并上传到hdfs上. 1.创建文件并写入数据: 2.上传hdfs 在hadoop权限下就 ...

  7. 转:spring的启动过程-spring和springMVC父子容器的原理

    要想很好理解这三个上下文的关系,需要先熟悉spring是怎样在web容器中启动起来的.spring的启动过程其实就是其IoC容器的启动过程,对于web程序,IoC容器启动过程即是建立上下文的过程. s ...

  8. day01_雷神_Python入门

    day01 1.编程语言 主流的像C.java.python.php.C#.等,可以从不同维度分类如下: 机器码和字节码 机器码: C 字节码: 其他 note: 机器码是电脑的CPU可直接解读的数据 ...

  9. centos下完全卸载mysql(别人写的,我仅仅为了学习记录)

    yum方式安装的mysql 1.yum remove mysql mysql-server mysql-libs compat-mysql51 2.rm -rf /var/lib/mysql 3.rm ...

  10. ASP 基础一

    ASP是什么? •ASP代表Active Server Pages(动态服务器页面) •需在IIS中运行的程序 我自己的理解就是UI和逻辑代码同在一个页面中,而缺点就是不易维护.code-Behind ...