如果在前台, 我需要获取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. A Magic Lamp -- hdu -- 3183

    http://acm.hdu.edu.cn/showproblem.php?pid=3183 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)   ...

  2. uri.php

    <?php /** * */ class URI { function _fetch_uri_string() { if(strtoupper($uri_protocol) == 'AUTO') ...

  3. vcpkg-微软开发的VC++打包工具

    vcpkg-VC++打包工具 1. 介绍 VCPKG,是VC++ Packaging Tool. 是微软 C++ 团队开发的在 Windows 上运行的 C/C++ 项目包管理工具,可以帮助您在 Wi ...

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

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

  5. hdu 4542 打表+含k个约数最小数

    http://acm.hdu.edu.cn/showproblem.php?pid=4542 给出一个数K和两个操作 如果操作是0,就求出一个最小的正整数X,满足X的约数个数为K. 如果操作是1,就求 ...

  6. execl 导出

    /** * 导出   是把数表中的数据添加到execl表中 */ public function export(){ $xlsData = Db('user')->select(); Vendo ...

  7. bootstrap1.1

    <!DOCTYPE html>   <html>   <head>   <meta charset="utf-8" />   < ...

  8. 取得cxgrid的表格的值,仔细看下面的代码

    procedure TfrmMain.cxGridDBTableView_List_PSSJCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas ...

  9. php开发工具,zendstudio13使用方法补丁

    官网原版下载http://downloads.zend.com/studio ... win32.win32.x86.exe 破解补丁:链接:http://pan.baidu.com/s/1gdi4U ...

  10. [leetcode 12] Inter to Roman

    1 题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...