Thymeleaf - 如何在Thymeleaf标记的html中将字符串与请求参数进行比较“th:if”? [英] Thymeleaf - How to compare string with request parameter in html in Thymeleaf tag "th:if"?

查看:3323
本文介绍了Thymeleaf - 如何在Thymeleaf标记的html中将字符串与请求参数进行比较“th:if”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to compare string with request parameter in html in Thymeleaf tag "th:if" ? right now i am using this

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

但是没有运气,这是行不通的。

But no luck, it is not working.

推荐答案

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

It's not working because param.error is array of strings. You must retrieve first element of array (param.error[0]) to get first value of parameter (see documentation). Besides that you can access request parameter via Web context object method #httpServletRequest.getParameter that returns first value when parameter is multivalued (see documentation).


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

  1. Usage of Web context namespaces for request attributes

<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上下文对象的使用情况

  • Usage of Web context object

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


  • 这篇关于Thymeleaf - 如何在Thymeleaf标记的html中将字符串与请求参数进行比较“th:if”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆