【问题标题】:HTML Element not rendering inside Thymeleaf conditional elementHTML 元素未在 Thymeleaf 条件元素中呈现
【发布时间】:2020-04-11 13:25:22
【问题描述】:

这是我第一次使用 thymeleaf,如果用户输入错误的凭据,我会使用 Spring 将 flash 属性返回到登录页面。 将显示错误消息(2 个div's),但不会呈现内部的<span class="closeBtn">。

源代码

<div th:if="${message}" class="row">
    <div th:text="${message}" th:class="${'col s8 offset-s2 m6 offset-m3 alert ' + alertClass}">
        <span class="closebtn">&times;</span>
    </div>
</div>

消息的右侧应该有一个“x”。我打开了浏览器检查器,它也没有显示在 HTML 中。

浏览器检查器代码

<div class="row">
    <div class="col s8 offset-s2 m6 offset-m3 alert error">Incorrect username / password.</div>
    ::after
</div>

【问题讨论】:

    标签: html thymeleaf


    【解决方案1】:

    基本上&lt;span class="closebtn"&gt;&amp;times;&lt;/span&gt; 被th:text="${message}" 覆盖。解决方案是使用th:utext 并添加&lt;span class="closebtn"&gt;&amp;times;&lt;/span&gt; 以及您的错误消息。
    即在属性文件中定义属性如view.error=Incorrect username/password. &lt;span class="closebtn"&gt;&amp;times;&lt;/span&gt; 并像使用它一样

    <div th:text="#{view.error}" th:class="${'col s8 offset-s2 m6 offset-m3 alert ' + alertClass}"></div>
    

    希望这会有所帮助。

    【讨论】:

    • 我还在我的配置类中添加了this method 并将messages.properties 放在我的资源文件夹的根目录中,并且修复了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 2023-03-09
    • 1970-01-01
    • 2021-12-09
    • 2015-10-27
    • 2022-10-04
    • 1970-01-01
    相关资源
    最近更新 更多