Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring thymeleaf text next line

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.

like image 982
Jacel Avatar asked Apr 16 '18 04:04

Jacel


People also ask

Is Thymeleaf fully integrated with Spring?

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.

Is Thymeleaf server side or client side?

Thymeleaf is a modern server-side Java template engine for both web and standalone environments.

Is Thymeleaf widely used?

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 Answers

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>
like image 63
Metroids Avatar answered Sep 23 '22 18:09

Metroids