I would like to pass on a message from my controller back to my thyme leaf template and set the value on mutiple lines.
in my controller , i set a text like this.
String message="item A \n item B \n Item C \n Item D"; //it could be 4 values, could 10 values.
modelAndView.addObject("successMessage", message);
In my thymeleaf template, i set it back like this.
<p th:utext="${successMessage}"></p>
I want it to be displayed like
item A
item B
item C
item D
in the front end, not all in one line. how do i do it? thanks alot.
It provides full integration with Spring Framework. It applies a set of transformations to template files in order to display data or text produced by the application. It is appropriate for serving XHTML/HTML5 in web applications. The goal of Thymeleaf is to provide a stylish and well-formed way of creating templates.
Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
But Thymeleaf's popularity is on a steady rise. The developer community is slowly moving away from 'once a common' MVC framework for Javascript-based development.
1) My suggestion would be to pass it back as an array.
List<String> messages = Arrays.asList("item A", "item B", "Item C", "Item D");
modelAndView.addObject("successMessages", messages);
HTML:
<p th:each="message: ${messages}" th:text="${message}"></p>
2) If you don't want to do that, you can of course use th:utext, but you'd have to use <br />
instead of \n
.
3) Another option would be to continue to use \n
and use white-space: pre
in your css.
<p style="white-space: pre;" th:text="${successMessage}"></p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With