Thymeleaf - How to compare string with request parameter in html in Thymeleaf tag “th:if”?

前端 未结 2 445
鱼传尺愫
鱼传尺愫 2021-02-01 08:36

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

相关标签:
2条回答
  • 2021-02-01 08:39

    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. 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>
      
    2. 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>
      
    0 讨论(0)
  • 2021-02-01 08:54

    With Thymeleaf 3, I normally use #request (a short form of #httpservletrequest) and #strings.equals() for that, which will look like this:

    <div th:if="${#strings.equals(#request.getParameter('error'), 'badCredentialsException')}"></div>
    
    0 讨论(0)
提交回复
热议问题