Thymeleaf-如何在Thymeleaf标签“ th:if”中将字符串与html中的请求参数进行比较?


问题内容

如何在Thymeleaf标签“ th:if”中将字符串与html中的请求参数进行比较?现在我正在使用这个

<div class="error" th:if="${param.error == 'badCredentialsException'}" th:with="errorMsg=#{login.badCredentials}">                      
     <p class="errorMsg"><span th:text="${errorMsg}"></span></p>
</div>

但是没有运气,它没有用。


问题答案:

它不起作用,因为param.error是字符串数组。您必须检索数组(param.error[0])的第一个元素以获取参数的第一个值(请参阅文档)。除此之外,您还可以通过Web上下文对象方法访问请求参数#httpServletRequest.getParameter,该方法在参数为多值时返回第一个值(请参阅文档)。

  1. Web上下文名称空间用于请求属性的用途

    <div class="error" th:if="${param.error[0] == 'badCredentialsException'}" th:with="errorMsg=#{login.badCredentials}">                      
    <p class="errorMsg"><span th:text="${errorMsg}"></span></p>