Springboot Thymeleaf模板文件调用Java类静态方法

所属分类: 软件编程 / Java编程 阅读数: 164
收藏 0 赞 0 分享

在模板文件的表达式中,可以使用“${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;

@Controller
public 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 1
hello world 2

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java经验点滴:处理没有被捕获的异常

Java经验点滴:处理没有被捕获的异常
收藏 0 赞 0 分享

Java经验点滴:类注释文档编写方法

Java经验点滴:类注释文档编写方法
收藏 0 赞 0 分享

Springboot Thymeleaf字符串对象实例解析

这篇文章主要介绍了Springboot Thymeleaf字符串对象实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Springboot Thymeleaf数字对象使用方法

这篇文章主要介绍了Springboot Thymeleaf数字对象使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Springboot Thymeleaf模板文件调用Java类静态方法

这篇文章主要介绍了Springboot Thymeleaf模板文件调用Java类静态方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

JAVA+Hibernate 无限级分类

主要看menu_id和parent_id这两个字段。 Eclipse生成的表持久映射:(说明:自己加level属性,作用:为了记录种类所在深度)
收藏 0 赞 0 分享

Tomcat数据源配置方法_JBuilder中

今天帮一同事配置一个数据源,采用tomcat5.5.9,本来是个很简单的事,以前也配过,但由于很长时间没用过容器提供的数据源了(IOC用惯了),也只记的个大概了,所以刚开始一配就出错了,google了一下,有很多资料,照着试试却都不好使(到不是别人说的不对,只是大家用的版本不同)
收藏 0 赞 0 分享

SWT(JFace)体验之ApplicationWindow

SWT(JFace)体验之ApplicationWindow
收藏 0 赞 0 分享

SWT(JFace) 体验之FontRegistry

测试代码如下:
收藏 0 赞 0 分享

SWT(JFace)体验之打开多个Form

SWT(JFace)体验之打开多个Form的实现代码。
收藏 0 赞 0 分享
查看更多