Question

I am a beginner with ThymeLeaf and have not used SpEL too much except for @PreAuthorize annotations, so please be so kind to help me out.

I am using ThymeLeaf (version 2.1.2) together with Spring (4.0.2.RELEASE) and the thymeleaf-spring4 package which (as far as I understood it) replaces the default OGNL scripting with SpEL.

What I want to achieve is simply that a localized string is capitalized via #strings.capitalize function. Here is what I tried so far:

<h1 th:text="#{retrievable.key}">Text to be replaced</h1>

Works perfectly and gives the expected result.

Now when I tried this:

<h1 th:text="${#strings.capitalize(#{retrievable.key})}">Text to be replaced</h1>

I got the following exception (root cause, rest omitted for clarity):

org.springframework.expression.spel.SpelParseException:
EL1043E:(pos 21): Unexpected token.  Expected 'identifier' but was 'lcurly({)'

Ok, fine. Just for fun, I omitted the curly brackets and got what I expected: the <h1> was empty.

So now I thought that it might necessary to preprocess the retrieval of the message for retrievable.key so that it is already evaluated when #strings.capitalize is evaluated. Though this seemed bot either counterintuitive and illogical to me, as this would break all programming rules, I tried that approach. It did not work either: using

${#strings.capitalize(__#retrievable.key__)}

lead to

org.thymeleaf.exceptions.TemplateProcessingException:
Could not parse as expression: "#retrievable.key"

and using

${#strings.capitalize(__#{retrievable.key}__)}

led to (you guessed it) <h1></h1>.

I know that the actual problem can be solved with CSS or JavaScript, but it's not necessarily about uppercasing or capitalizing, but on processing of localized strings and this is an example.

So what am I missing here?

Solution provided by Thymeleaf Forum

Zemi of the ThymeLeaf Forum provided the following, elegant solution:

<h1 th:text="${#strings.capitalize('__#{retrievable.key}__')}">Text to be replaced</h1>

Please notice the single quotes. Preprocessing seems to really mean preprocessing in Thymeleaf.

I have accepted the first working answer, however.

Was it helpful?

Solution

The following worked for me

<body th:with="message=#{retrievable.key}">
    <h1 th:text="${#strings.capitalize(message)}">Text to be replaced</h1>
</body>

OTHER TIPS

As you wrote it looks like preprocessed expressions ala

__${...}__

do not work with the hashtag.

What you can do instead is using the messageSource bean inside your expression as this can be resolved in a "normal" expression as anything else.

 <div th:text="${beans.messageSource.getMessage(messageVariable)}"></div>

Why would you capitalize a localized string? If it were truly localized to different languages then you might not get the results you want if you do a toUpper on them. The better approach is to localize the strings in the case that you want for display.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top