【问题标题】:spring thymeleaf text next linespring thymeleaf 文本下一行
【发布时间】:2018-04-16 04:52:13
【问题描述】:

我想将一条消息从我的控制器传递回我的百里香叶模板,并在多行上设置值。

在我的控制器中,我设置了这样的文本。

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

在前端,而不是全部在一行中。我该怎么做?非常感谢。

【问题讨论】:

    标签: html spring thymeleaf


    【解决方案1】:

    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,但您必须使用 &lt;br /&gt; 而不是 \n

    3) 另一种选择是继续使用 \n 并在您的 css 中使用 white-space: pre

    <p style="white-space: pre;" th:text="${successMessage}"></p>
    

    【讨论】:

    • #3 是最好的答案
    • @metroids, #2 th:utext 对我有用,我从 java 端添加了
      并根据需要逐行打印所有这些。谢谢!
    • @Metroids,#2 建议非常有效。这是我的情况:&lt;td th:utext="${category.nameEn} + (${#strings.isEmpty(category?.descriptionEn)} ? '' : ('&lt;br&gt;&lt;br&gt;' + ${category.descriptionEn}))"&gt;&lt;/td&gt;
    • #3 对前景不利
    猜你喜欢
    • 1970-01-01
    • 2018-02-08
    • 2023-03-04
    • 1970-01-01
    • 2019-10-29
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多