如何将 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]
)获取参数的第一个值(参见文档 http://www.thymeleaf.org/doc/html/Using-Thymeleaf.html#web-context-namespaces-for-requestsession-attributes-etc.)。除此之外,您还可以通过 Web 上下文对象方法访问请求参数#httpServletRequest.getParameter
当参数为多值时返回第一个值(请参阅文档 http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter%28java.lang.String%29).
-
使用请求属性的 Web 上下文命名空间
<div class="error" th:if="${param.error[0] == 'badCredentialsException'}" th:with="errorMsg=#{login.badCredentials}">
<p class="errorMsg"><span th:text="${errorMsg}"></span></p>
</div>
-
Web 上下文对象的用法
<div class="error" th:if="${#httpServletRequest.getParameter('error') == 'badCredentialsException'}" th:with="errorMsg=#{login.badCredentials}">
<p class="errorMsg"><span th:text="${errorMsg}"></span></p>
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)