SpringBoot——Thymeleaf基本表达式基本对象、功能对象

1 Thymeleaf基本表达式基本对象

模板引擎提供了一组内置的对象,这些内置的对象可以直接在模板中使用,这些对象由#号开始引用,我们比较常用的内置对象。

主要的基本对象有两个;#request和#session

#request 相 当 于 httpServletRequest 对 象 , 这 是 3.x 版 本 , 若 是 2.x 版 本 使 用。#httpServletRequest,在页面获取应用的上下文根,一般在 js 中请求路径中加上可以避免 404。

相当于 HttpSession 对象,这是 3.x 版本,若是 2.x 版本使用#httpSession。在后台方法中向 session 中放数据。

 controller类

package com.liuhaiyang.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController1 {
    @RequestMapping("/index")
    public String  idnex(HttpServletRequest request, Model model){
    model.addAttribute("username","lisi");
    request.getSession().setAttribute("data","sessionData");
        return "index";
    }
}

核心配置文件

spring.thymeleaf.cache=false

前端展示数据页面index.html


<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3>从ssession中获取值</h3>
获取session值的第一种方法:<span th:text="${#session.getAttribute('data')}"></span><br/>
获取session值的第二种方法:<span th:text="${#httpSession.getAttribute('data')}"></span><br/>
获取session值的第三种方法:<span th:text="${session.data}"></span><br/>
<hr/>
<script type="text/javascript" th:inline="javascript">
    //获取协议名称
    var scheme = [[${#request.getScheme()}]];
    //获取服务器ip地址
    var serverName = [[${#request.getServerName()}]];
    //获取服务器端口号
    var serverPort = [[${#request.getServerPort()}]];
    //获取上下文根
    var contextPath = [[${#request.getContextPath()}]];
    //将以上四个变量拼接成一个完成路径
    var allPath = scheme + "://" + serverName + ":" + serverPort + contextPath;
    alert(allPath);

    var requestURL = [[${#httpServletRequest.requestURL}]];
    var queryString = [[${#httpServletRequest.queryString}]];
    var requestAddress = requestURL + "?" + queryString;
    alert("该请求路径为:" + requestAddress);
</script>
</body>
</html>

启动入口类查看结果

package com.liuhaiyang.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootTest21Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootTest21Application.class, args);
    }

}

 

 

 

 2 Thymeleaf基本表达式功能对象

模板引擎提供的一组功能性内置对象,可以在模板中直接使用这些对象提供的功能方法工作中常使用的数据类型,如集合,时间,数值,可以使用 Thymeleaf 的提供的功能性对象
来处理它们内置功能对象前都需要加#号,内置对象一般都以 s 结尾

官方手册:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

#dates: java.util.Date 对象的实用方法: <span th:text="${#dates.format(curDate, 'yyyy-MM-dd HH:mm:ss')}"></span>

#calendars: 和 dates 类似, 但是 java.util.Calendar 对象;

#numbers: 格式化数字对象的实用方法;

#strings: 字符串对象的实用方法: contains, startsWith, prepending/appending 等;

#objects: 对 objects 操作的实用方法;

#bools: 对布尔值求值的实用方法;

#arrays: 数组的实用方法;

#lists: list 的实用方法,比如<span th:text="${#lists.size(datas)}"></span>

#sets: set 的实用方法;

#maps: map 的实用方法;

#aggregates: 对数组或集合创建聚合的实用方法;

下面举几个简单的说明一下Date、String的一些方法写一个例子

controller类

package com.liuhaiyang.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
public class UserController2 {
    @RequestMapping("/function")
    public String function(Model model){
        model.addAttribute("time",new Date());
        model.addAttribute("data","springbootHelloWorld");

        return "function";
    }
}

前端function.html代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${time}"></div>
<div th:text="${#dates.format(time,'yyyy-MM-dd HH:mm:ss')}"></div>
<div th:text="${data}"></div>
<div th:text="${#strings.substring(data,0,10)}"></div>  <!--包括0,不包括10-->
</body>
</html>

其他代码复用上面的

结果截图:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值