I'm using Spring 3 and Thymeleaf to make some webpages and I am lost as for how to show messages like this:
welcome.message=Hello {0}, welcome!
and then replace {0} with the user name inside thymeleaf tags:
<h1 th:text="#{welcome.message}">Welcome Placeholder</h1>
I'm not even sure if {0} is the right syntax for the bundle message.
You can use
#{welcome.message(${some.attribute})}
where some.attribute would be the value to use when replacing {0}.
You should be able to comma separate the values between the () to add more values to be used.
You can even use a calculated message key as a parameter:
<p th:text="#{messages.msg1(${param1})}"></p>
<p th:text="#{messages.msg2(${param2},${param3})}"></p>
<p th:text="#{messages.msg3(#{${param4}})}"></p>
Above, the parameter of [msg3] is a message key [#{key}] where key is itself calculated [${param4}]. The benefit is that you can insert internationalized calculated fragments in an internationalized message.
If you need to pass an array of parameters where you don't know the size of the array then you can use:
<p th:text="${#messages.msgWithParams(messageKey, messageParams)}"></p>
<!-- or -->
<p th:text="${#messages.msgOrNullWithParams(messageKey, messageParams)}"></p>
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#messages-1
Related
Hi I'm trying to hide tags wen the message is empty but I can't seem to figure out how, for what I read if I want to hide and empty variable all I need to do is either with "unless" or "if":
<p th:unless="${#strings.isEmpty(myVar.theEmptyVar)}">
But when I use the message type var It doesn't seem to work the code fails
<p th:unless=""{#strings.isEmpty(messageVar)}">
The closest I got is this but it won't show anything:
<p th:if="${!#strings.isEmpty('[[#{label}]]')}" >
I've been going up and down the thymeleaf documentation and google but I can't seem to find a solution that works, thanks.
Assuming you have label, which is an empty entry in a messages file, like this:
label=
...then you can use the following in your Thymeleaf template:
<p th:if=" ! ${#strings.isEmpty(#messages.msg('label'))}">
my text
</p>
The ! is a negation ("not"). Therefore, because the message for label is empty, you will not see "my text" in the web page, and the <p> tag will not be created.
If you remove the ! then you will see "my text", inside the <p> tag.
You cannot use #{label} directly in this specific case. You have to wrap it in the #messages.msg() method.
Also, if the label message does not exist in the message file, then this will behave the same way as label=.
I am using Thymeleaf as the front end.
I have some menu categories in natural language that I display on a web page and pass to the server.
For example, I have a category of "My favourite cats"
The category is in a variable ${category.key}
This category has a link;
<a th:href="|http://myserver?selectedCategory=${category.key}|><span th:text=${category.key}></span></a>
If I do not URLEncode ${category.key} on the server, then when a
user clicks the link, the selectedCategory parameter is null if there
is a whitespace in the category string.
If I encode the category string, the selectedCategory parameter
passes to the server fine BUT the link text appears as
My+favourite+cats
I don't want two variables, one encoded, one not encoded.
How do I either encode or unencode ${category.key} as part of the Thymeleaf HTML compilation process?
#symbol is Server Context path in Thymeleaf. you use #symbol.
<a href="#" th:href="#{/your server context path?selectedCategory=__${category.key}__}">
<span th:text="${category.key}"> </span></a>
Thymeleaf will url encode strings if you build them with url syntax (which was supplied in a comment above). For your example url, it should look like this -- using the # symbol instead of the $:
<a th:href="#{http://myserver(selectedCategory=${category.key})}" th:text="${category.key}" />
(Also, you don't need that extra span.)
Using the #symbol is definitely the primary alternative. But just to note another option that may be of use on occasions:
The answer is to use the lesser know #strings functions to replace the '+' with ' '
In the link text, use
th:text="${#strings.replace(category.key,'+',' ')}"
I have a string which contains raw data, which I want escaped. The string also contains markers which I want to replace with span tags.
For example my string is
"blah {0}something to span{1} < random chars <"
I would like the above to be rendered within a div, and replace {0} with and {1} with
I have tried a number of things, including doing the substitution in my controller, and trying to use the th:utext attribute, however I then get SAX exceptions.
Any ideas?
You can do this using i18n ?
something like:
resource.properties:
string.pattern=my name is {0} {1}
thymeleaf view:
<label th:text="#{__${#string.pattern('john', 'doe')}__}"></label>
The result should be:
my name is john doe
Im not sure this is a good way. But I hope it could help you
It looks using message parameters is the right approach to output formatted strings. See http://www.thymeleaf.org/doc/usingthymeleaf.html#messages
I suspect you need to pass character entity reference in order to avoid SAX exceptions
<span th:utext = "#{string.pattern(${'<span>john</span>'}, ${'<span>doe</span>'})}"/>
Alternatively place the markup in your .properties file:
string.pattern=my name is <span>{0}</span> <span>{1}</span>
When I try to put some dynamic parts in my texts to be used by <s:text> tag in Struts 2, these parts are replaced by the params I defined.
Here is how I write my sentences in my file.properties:
my_error=The event {0} doesn't exist
Here is how I try to display it:
<s:text name="my_error">
<s:param>Event01</s:param>
</s:text>
But in the result, the expression {0} is NOT replaced and I have no error in the log. What's wrong?
I really can't understand. I picked this example, so I have this in my jsp file:
<s:text name="msg.error">
<s:param >Event01</s:param>
</s:text>
<br />
<s:text name="name.msg.param" >
<s:param >mkyong</s:param>
</s:text>
and this in my .properties:
msg.error = This event doesn't exist: {0}
name.msg.param = This is a message from properties file - param : {0}
But the result is:
This event does not exist: {0}
This is a message from properties file - param : mkyong
I do not manage to find a real difference.
The message must be in a resource bundle with the same name as the
action that it is associated with.
If the named message is not found in a property file, then the body of
the tag will be used as default message.
Create or put the resource bundle for the locale you use to the place better described using the search order in the localization guide.
i guess {0} refers to the first index of the list that should be passed as a parameter to the getText method.
Now when you use s:text and pass the param, it should be a list type variable containing the element with value "Event01" in the first index.
Try implementing the same. It might work :)
I need to pull all teh messages from a table and display on the jsp page. I have the code like below:
I have the list of messages stored as :
SimpleStringVO string value: Welcome to the XYZ Tool homepage. ,SimpleStringVO string value: Here you can enter your account number ,SimpleStringVO string value: ,SimpleStringVO string value: thank you
When I tried to display this in jsp page, the message is not formated as it is stored. "thank you" comes immediately after the 2nd string. I need to display the space string and then the "thank you" string. (3rd string has just spaces) My code in Jsp is like this :
<td><logic:iterate name="AllNewsCashe" id="news" type="com.fw.valueobject.SimpleStringVO">
<bean:write name="news" property="stringValue"/>
</logic:iterate>
</td>
how to display thise messages as it is without formating ?
If you need to force a space in HTML, you will need to store it as instead of " ". However, since are you using bean:write tag, you can probably use filter property to retain " ":-
<bean:write name="news" property="stringValue" filter="false" />
Here's the description of filter from Struts documentation:-
If this attribute is set to true, the
rendered property value will be
filtered for characters that are
sensitive in HTML, and any such
characters will be replaced by their
entity equivalents.