【发布时间】:2014-04-30 22:52:06
【问题描述】:
我是 ThymeLeaf 的初学者,除了 @PreAuthorize 注释外,没有过多使用 SpEL,所以请帮帮我。
我正在使用 ThymeLeaf(版本 2.1.2)与 Spring(4.0.2.RELEASE)和 thymeleaf-spring4 包(据我了解)replaces the default OGNL scripting with SpEL。
我想要实现的只是通过#strings.capitalize 函数将本地化字符串大写。到目前为止,这是我尝试过的:
<h1 th:text="#{retrievable.key}">Text to be replaced</h1>
完美运行并提供预期结果。
现在当我尝试这个时:
<h1 th:text="${#strings.capitalize(#{retrievable.key})}">Text to be replaced</h1>
我得到了以下异常(根本原因,为清楚起见省略其余部分):
org.springframework.expression.spel.SpelParseException:
EL1043E:(pos 21): Unexpected token. Expected 'identifier' but was 'lcurly({)'
好的,好的。只是为了好玩,我省略了大括号并得到了我的预期:<h1> 是空的。
所以现在我认为可能有必要对retrievable.key 的消息检索进行预处理,以便在评估#strings.capitalize 时已经评估它。尽管这对我来说似乎违反直觉和不合逻辑,因为这会破坏所有编程规则,但我尝试了这种方法。它也不起作用:使用
${#strings.capitalize(__#retrievable.key__)}
导致
org.thymeleaf.exceptions.TemplateProcessingException:
Could not parse as expression: "#retrievable.key"
并使用
${#strings.capitalize(__#{retrievable.key}__)}
导致(你猜对了)<h1></h1>。
我知道实际问题可以用 CSS 或 JavaScript 解决,但不一定是大写或大写,而是本地化字符串的处理,这是一个例子。
那么我在这里错过了什么?
Thymeleaf 论坛提供的解决方案
ThymeLeaf 论坛的 Zemi 提供了以下内容,elegant solution:
<h1 th:text="${#strings.capitalize('__#{retrievable.key}__')}">Text to be replaced</h1>
请注意单引号。预处理似乎真的意味着 Thymeleaf 中的预处理。
不过,我已经接受了第一个可行的答案。
【问题讨论】:
-
感谢您的准确问题和答案!我在使用 strings.replace 时遇到了完全相同的问题,你帮了我很多! :)
标签: java spring localization thymeleaf spring-el