spring boot 与 thymeleaf (4): 基本对象、工具类对象
如果在前台, 我需要获取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): 基本对象、工具类对象的更多相关文章
- Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- 极简 Spring Boot 整合 Thymeleaf 页面模板
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- 一个小demo熟悉Spring Boot 和 thymeleaf 的基本使用
目录 介绍 零.项目素材 一. 创建 Spring Boot 项目 二.定制首页 1.修改 pom.xml 2.引入相应的本地 css.js 文件 3.编辑 login.html 4.处理对 logi ...
- Spring Boot整合Thymeleaf模板引擎
什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...
- 兼容 Spring Boot 1.x 和 2.x 配置类参数绑定的工具类 SpringBootBindUtil
为了让我提供的通用 Mapper 的 boot-starter 同时兼容 Spring Boot 1.x 和 2.x,增加了这么一个工具类. 在 Spring Boot 中,能够直接注入 XXProp ...
- Spring Boot整合 Thymeleaf 模板引擎
什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例
Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...
- spring boot 与 thymeleaf (2): 常用表达式
在asp.net mvc 中, 有一个视图解析器, 可以支持Razor语法. 使用起来, 是非常的方便, 并且, 写在前台页面的后台方法, 是可调试的. 但是在java中, 目前我还没有接触到, 像. ...
- Spring Boot 2 + Thymeleaf:表单字段绑定、表单提交处理
Spring Boot中Thymeleaf对表单处理的一些用法:(1)使用th:field属性:进行表单字段绑定(2)使用ids对象:一般用于lable配合radio或checkbox使用(3)表单提 ...
随机推荐
- C语言四则运算编程
#include<stdio.h> #include<stdlib.h> void main() { int c,e,f; int x; while(1) { x=rand() ...
- 18、标准IO库详解及实例
标准IO库是由Dennis Ritchie于1975年左右编写的,它是Mike Lestbain写的可移植IO库的主要修改版本,2010年以后, 标准IO库几乎没有进行什么修改.标准IO库处理了很多细 ...
- hdu 4970 trick
http://acm.hdu.edu.cn/showproblem.php?pid=4970 有n个格子在一条线标号1-n上,可以给范围在l到r内的格子架上攻击力为d的攻击塔,有m个怪物,每个怪物有个 ...
- vi 命令
学习的网址:http://www.cnblogs.com/88999660/articles/1581524.html vi filename: 打开或新建 vi +n filename: 光标置于第 ...
- Python自动化开发 - Python操作MySQL
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy 一.pymysql pymsql是Python中操作MySQL的模块,其使用方法和mysq ...
- ORACLE EBS常用表
http://www.cnblogs.com/quanweiru/archive/2012/09/26/2704628.html call fnd_global.APPS_INITIALIZE(131 ...
- Delphi TStringHelper用法详解
Delphi TStringHelper用法详解 (2013-08-27 22:45:42) 转载▼ 标签: delphi_xe5 it 分类: Delphi Delphi XE4的TStringHe ...
- SQL SERVER的锁机制(一)——概述(锁的种类与范围)
锁定:通俗的讲就是加锁.锁定是 Microsoft SQL Server 数据库引擎用来同步多个用户同时对同一个数据块的访问的一种机制. 定义:当有事务操作时,数据库引擎会要求不同类型的锁定,如相关数 ...
- 阿里云oss服务通用类
在webconfig中配置信息 <?xml version="1.0" encoding="utf-8"?><configuration> ...
- AEAI DP开发统计分析
1 背景概述 平时做统计分析都是调rest服务,给前台提供数据,然后在管理控制台里配置portlet.但并不是所有的项目都会用到portal,这时就需要在AEAI DP应用开发平台里开发统计分析了,下 ...