首页 > 编程语言 >Thymeleaf常用语法:模板文件中表达式调用Java类的方法

Thymeleaf常用语法:模板文件中表达式调用Java类的方法

时间:2023-02-17 19:44:14浏览次数:58  
标签:调用 return String demo Thymeleaf Java public 模板

在模板文件的表达式中,可以使用“${T(全限定类名).方法名(参数)}”这种格式来调用Java类的静态方法。

开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。

1、pom.xml
加入Thymeleaf依赖

        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>

2、src/main/java/com/example/demo/TestUtils.java

package com.example.demo; public class TestUtils {    public static String toUpperCase(String s){        return s.toUpperCase();    }}

3、src/main/java/com/example/demo/TestController.java

package com.example.demo; import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic class TestController {    @RequestMapping("/")    public String test(){        return "test";    }     public static String toLowerCase(String s){        return s.toLowerCase();    }}

4、src/main/resources/templates/test.html

<div th:text="${T(com.example.demo.TestUtils).toUpperCase('hello world 1')}"></div><div th:text="${T(com.example.demo.TestController).toLowerCase('HELLO WORLD 2')}"></div> 

浏览器访问:http://localhost:8080
页面输出:

HELLO WORLD 1hello world 2

thymeleaf中调用Java方法

将需要调用的类进行注册便可以在thymeleaf中进行调用

Java 代码

@Component("module")
public class ModuleService {
	private final BizSiteInfoService siteInfoService;
	private final BizArticleService bizArticleService;
	// some codes...
	public Object get(String moduleName) {
        switch (moduleName) {
            //热门文章
            case "hotList":
                return bizArticleService.hotList(CoreConst.PAGE_SIZE);
            //网站信息统计
            case "siteInfo":
                return siteInfoService.getSiteInfo();
        }
    }
}
12345678910111213141516

tymeleaf代码

获取对象

<div>文章总数:<span th:text="${@module.get('siteInfo').articleCount}"></span> 篇</div>
1

获取列表

<ol class="article-list">
    <li th:each="item,temp:${@module.get('hotList')}"  class="slide">
        <span th:text="${temp.index+1}"></span>
        <a th:text="${item.title}" th:href="${'/blog/article/'+item.id}"></a>
    </li>
</ol>
123456

标签:调用,return,String,demo,Thymeleaf,Java,public,模板
From: https://www.cnblogs.com/xiondun/p/17131371.html

相关文章

  • java在线读取Excel内容
    本示例采用Springboot的Thymeleaf做前台展示,核心还是java代码,想了解Thymeleaf的可以点击​​《SpringBoot入门十六,添加Thymeleaf模板支持》​​进行入门学习,这里就只做关于......
  • Java Web(二)MyBatis
    MyBatis一.MyBatis简介1.什么是MyBatisMyBatis是一款优秀的持久层框架,用于简化JDBC开发MyBatis本是Apache的一个开源项目iBatis,2010年这个项目由apachesoftwarefoundati......
  • 安装netbense时提示在此计算机中找不到Java SE开发工具包(JDK)
    在提示信息中显示的需要JDK8或更高版本,这边需要提醒大家不要把jdk8和jdk1.8弄混首先大家打开控制面板找到程序并点击进去点击程序和功能  在电脑上查看是否有jav......
  • java jna框架免注册调用大漠插件及指针参数方法调用
    大漠dll说明1、dm.dll为com类型组件。2、DmReg.dll免注册为常规dll。学习研究时的历程1、因为大漠是com组件,所以可以使用jacob调用大漠。但是不支持免注册dll的使......
  • 在VScode中引入JAVASCript的方式
    打开VSCODE,创建一个html文件夹,shift+!快速创建代码。创建js文件,然后可以在里面定义变量,函数然后在html文件body中引入javaScript ......
  • javascript 高级编程系列 - 迭代器/生成器
    1.迭代器(Iterator)特殊对象,具有迭代过程的接口next()方法每次调用next()方法,返回一个结果对象结果对象有两个属性value(任何类型)和done(布尔类型)当集合的值返回完时......
  • 强记 分支结构语法 java 230217
    单路分支//格式if(条件){条件成立时要做的事情;}//例子intage=19;if(age>18){System.out.println("天天网吧欢迎你");}二路分支//格式if(条件){......
  • 用Java实现邮件发送 SimpleMailMessage
    网上很多用Session方式那种发送,有点过于冗余,需要在POM文件里添加依赖有一个文章更详细可以参考:https://www.php.cn/manual/view/22279.html<dependency><......
  • Java工具类Collections
    一、概述Collections工具类提供了大量针对Collection和Map的操作,都为静态(static)方法,总体可分为四类:排序操作查找、替换操作同步控制设置不可变(只读)集合二、排......
  • 求1到100的总和 java 230217
    新增一个变量s保存总结果给这个s初始化值为0每次得到循环的数字后就给这个变量s进行累加代码:publicclassTest{publicstaticvoidmain(String[]args){//......