johnsonshu
V2EX  ›  问与答

关于 thymeleaf 里调用类的 static method

By johnsonshu at 2021 年 10 月 20 日 · 1863 次点击

上网查了一下,基本都是用下面的方法, 但是 网页代码寸土寸金的, 这种写法也太丑陋了。 有什么优雅一点的解决办法吗?

<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>
第 1 条附言  ·  2021 年 10 月 21 日

谢谢建议,写完了。

Java端:

@Repository("stringUtil")
public class StringUtil {

Thymeleaf 端:

th:href="${@stringUtil.convert2Context(room.url)}
2 条回复  •  2021-10-21 17:18:42 +08:00
yangyaofei
   1
yangyaofei  
   2021 年 10 月 20 日 via Android   ❤️ 1
SpEl ? testutil 写个 bean,然后就可以直接调用了
cyndihuifei
   2
cyndihuifei  
   2021 年 10 月 21 日
当然是前后端分离啦
© 2026 V2EX · 35ms · 3.9.8.5