Custom formatted map in Thymeleaf view
I am trying to convert a map with a custom Formatter.
I created a @LongCurrency Annotation that converts long values like this:
Long → String
22 → 00.22
3310 → 33.10
String → Long
3 → 3
22.11 → 2211
( Custom formatting with Spring MVC annotation )
Everything is still working. Now I would like to convert the card with this formatting. Here are some pseudo codes to show what I am trying to accomplish.
@LongCurrency
private Map<Integer, Long> test;
//only to make clear what I am trying to do.
private Map<Integer, @LongCurrency Long> test;
The second way would be to use the transform utility object from Thymeleaf. http://www.thymeleaf.org/doc/thymeleafspring.html#the-conversion-service
I've tried something like this:
Controller:
model.addAttribute("test", 3333L);
Thymeleaf:
<td th:text="${#conversions.convert(${test},LongCurrency)}}"></td>
but that won't work. Errormessage : org.thymeleaf.exceptions.TemplateProcessingException : Could not parse expression: $ {# conversionions.convert ($ {test}, LongCurrency)}}
I would appreciate help or ideas in one or both ways.
EDIT1: "normal" annotated long value works
Bean:
@LongCurrency
private long test2;
Thymeleaf
<div th:text="${{test2}}" >
source to share
I have a third way to work for me. I have created a LongCurrencyService that calls the parse and print methods of my Formatter class.
@Named
@Singleton
public class LongCurrencyService {
public static String LongToStringCurrency(Long value) {
LongCurrencyFormatter longCurrencyFormatter = new LongCurrencyFormatter();
return longCurrencyFormatter.print(value, Locale.GERMAN);
}
public static Long StringToLongCurrency(String value) throws ParseException {
LongCurrencyFormatter longCurrencyFormatter = new LongCurrencyFormatter();
return longCurrencyFormatter.parse(value, Locale.GERMAN);
}
}
In my Thymeleaf, I use EL:
<h1 th:text="${@longCurrencyService.LongToStringCurrency(test)}"></h1>
which prints 33.33 as needed.
My way works for me, but I don't want me to accept my own answer. This is not an answer to my question, how to comment on a map using formatting.
source to share