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

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?

#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.

you can do it by
$string.toLowerCase() or $string.toUpperCase()

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,

Related

Calling messages.properties dynamically with controller model variable and Thymeleaf

I have my messages.properties like this:
tablas.menu.paises=Pa\u00EDses
tablas.menu.regiones=Regiones
tablas.menu.anhos=A\u00F1os
tablas.menu.universidades=Universidades
tablas.menu.usuarios=Usuarios
In the .html I have a menu showing each item with th:each:
<li class="nav-item" th:each="menu : ${menus}">
<a th:text="#{tablas.menu.${menu.nombre}}"></a>
</li>
For each "menu in ${menus}", menu.nombre has the values paises, regiones, anhos...
But thymeleaf doesn't recognise the model variable inside the #{}, and this it is giving me in the view the error when it doesn't find the message:
??tablas.menu.${menu.nombre}_es_ES??
Configuration is ok, if I change the call for #{tablas.menu.regiones} I get 'regiones' and so.
Is there any way to call a message from messages.properties dynamically using a model variable like this?
Thanks.
You can use the #messages utility object for this:
<a th:text="${#messages.msg('tablas.menu.' + menu.nombre)}" />
Or you can create the string using literal substitution:
<a th:text="#{|tablas.menu.${menu.nombre}|}" />
I would recommend using preprocessing only as a last resort, as bad values can cause runtime errors. (Although they do work for this.)

How to get value from input thymleaf and post to controller with th href spring boot

I have problem with thymleaf spring boot, my input is like this
<input class="-Text" type="text" style="border:none" placeholder="1234567890xx" th:value="${sn}" name="sn"/>
I want to get parse the value from input to my th href thymleaf code like this
<a id="btnx" th:href="#{/pages/input-order-manual/finish-confirm/?sn=__${sn}__}" class="btn btn-primary btn-lg btn-block">Confirm</a>
But when I try to run, this value always return null for my sn variable, what must I do, to make this code run?
For any help,
Thank you in advance
Insert full code. Run code in debug, add break point before return template and make sure you have sn variable in model. Also you can concat th:href (th:href="#{/pages/input-order-manual/finish-confirm/?} + ${sn}")

Translation in parameter of translation with Thymeleaf

Using Thymeleaf 3.0.9, suppose I have this translation:
hello=Hello {0}
In my template, I have:
<span th:text="#{hello(${user.getName()})}"></span>
Now suppose user.getName() can be null, so I want to display something nice in that case:
<span th:text="#{hello(${user.getName()?:'Unknown'})}"></span>
This works fine.
Now I also want to tranlate Unknown, how can I do this?
I tried:
<span th:text="#{hello(${user.getName()?:#{unknown})}"></span>
But that does not seem to work.
I think you can use the #messages utility object:
<span th:text="#{hello(${user.getName()?:#messages.msg('unknown')})}"></span>
And add property in the messages file(s):
unknown=Unknown

get incomplete POST data after form submit

i have been working on this problem for hours.
I have a form, with a textarea. I use the nicEdit texteditor. It replaces the textarea and shows a nice text editor, because i want my users to add some style to their content.
I use codeIgniter (PHP), and i use the form_helper to create the form. Also i use the form_validation for ss-validation and jquery validation for cs-validation
When i click submit, the form submits seemingly fine. I say this because i use fiddler (an http logger) and i see my text with the right html tags wrapped around it by the text editor.
but when i get the #_pots data in the view, somehow some part of the tags have been removed.
How fiddler traces the HTTP call and the submitted form data (seems correct)
Hello SO, <br><br>
<span style="font-weight: bold;">the following line should be bold</span><br><br>
<span style="font-style: italic;">the following line should be italic</span><br><br>
<span style="text-decoration: underline;">the following line should be underlined</span><br>
How my html looks in my view and in my print_r result from my #_post data
Hello SO,<br><br>
<span bold;"="">the following line should be bold</span><br><br>
<span italic;"="">the following line should be italic</span><br><br>
<span underline;"="">the following line should be underlined</span><br>
It looks like somehow, when i get my data back, it removes the style="font-weight
Does $_post do anything with special characters?!?! has someone experienced similar issues with this?
all responses are greatly appreciated.
You need extend the CI_Security class from Codeigniter and comment/remove/modify this line:
/*
if(in_array($_SERVER['REQUEST_URI'],$allowed))
{
$evil_attributes = array('on\w*', 'xmlns');
}
else
{
$evil_attributes = array('on\w*', 'style', 'xmlns');
}
*/

How to remove the style tag using asp.net

I want to remove all style attribute in html tags using asp.net...
string source=#" <div style="font-size: 12pt;"> Hello world</div> <style id=fll margin:19px auto;text-align:center"></style>";
I want the result like this:
<div>Hello world </div>
For that i am using,
string expn =#"(?i)<(table|tr|td)(?:\s+(?:""[^""]""|'[^']'|[^""'>])*)?>";
return System.Text.RegularExpressions.Regex.Replace(source, expn, string.Empty);
I dont know which one is using,
Tell me the query what i have to use for this one....
This should work (though I don't understand the style tag at the end of your example):
string source="<div style=\"font-size: 12pt;\"> Hello world</div>";
string pattern = "style=\".*\"";
string result = Regex.Replace(source, pattern, "");

Resources