Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I capitalize a string showed by Thymeleaf into a page?

I am working on a Spring MVC application that uses Thymeleaf as template engine and I am trying to capitalize some string showed into my page. On my page I have something like this:

<li class="com__nav-item" th:each="menuItem : ${#authentication.principal.listaFunzioniUtente}">
    <a href="" class="com__nav-link centered">
        <span class="blue-line animate scaleIn delay-3" style="font-size: 1.4em; text-align: center;" th:text="${#strings.capitalize(menuItem.desFnz)}"></span>
        <span class="white-circle animate scaleIn delay-5"></span>
    </a>
</li>

As you can see in the previous code, in the first <span> tag, I show a string inside the desFnz property of the menuItem object.

It works fine, my problem is that I want capitalize all the characters, so I tried to do:

th:text="${#strings.capitalize(menuItem.desFnz)}"

using the #strings.capitalize() but it can't work, in fact in my page I still obtain the text but not capitalized. Why? What am I missing? How can I fix this issue?

like image 733
AndreaNobili Avatar asked Mar 15 '16 15:03

AndreaNobili


People also ask

How do you capitalize a string?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.

Which method is used to capitalize all the letters of a string?

Java String toUpperCase() Method The toUpperCase() method converts a string to upper case letters.

Does string need to be capitalized?

The String type is capitalized because it is a class, like Object , not a primitive type like boolean or int (the other types you probably ran across).

How do you check for string equality in Thymeleaf?

You can even compare String objects in thymeleaf. Under the hood, Thymeleaf uses the String. compareTo() method from the Comparable interface. That is, You can compare any two objects of the same type if they are Comparable .


3 Answers

you can do it by

$string.toLowerCase() or $string.toUpperCase()

like image 188
Sanjay Makwana Avatar answered Nov 01 '22 15:11

Sanjay Makwana


#strings.capitalize(menuItem.desFnz) will only capitalize the 1st character, where as #strings.toUpperCase(menuItem.desFnz) will convert the entire string to uppercase. Here is the documentation for the Strings class.

like image 42
Pradeep Pati Avatar answered Nov 01 '22 17:11

Pradeep Pati


Just adding to Pradeep Pati's point. In case you are using it in the spring boot project where some of your values are coming from messages.properties

like In messages.properties file, You have something like:

email.dailyAlert.greeting.newTemplate = Dear {0},

Then to substitute the value in place of {0} (in the Title case), you need to write like the below line.

<p th:text="#{email.dailyAlert.greeting.newTemplate(${#strings.capitalize(orgSlug)})}"></p>

The final output will be:

Dear Organisation,


like image 28
Rahul Kumar Avatar answered Nov 01 '22 16:11

Rahul Kumar