我想将一条消息从我的 Controller 传递回我的 thymeleaf 模板,并在多行上设置值。
在我的 Controller 中,我设置了这样的文本。
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);
在我的 thymeleaf 模板中,我将它设置为这样。
<p th:utext="${successMessage}"></p>
我想让它显示成这样
item A
item B
item C
item D
在前端,不是都在一行。我该怎么做?非常感谢。
最佳答案
1) 我的建议是将其作为数组传回。
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) 如果你不想这样做,你当然可以使用 th:utext,但你必须使用 <br />
而不是 \n
.
3) 另一种选择是继续使用 \n
并使用 white-space: pre
在你的 CSS 中。
<p style="white-space: pre;" th:text="${successMessage}"></p>
关于html - spring thymeleaf 文字下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49849839/