Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf - How to compare string with request parameter in html in Thymeleaf tag "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.

like image 282
user3515080 Avatar asked Apr 23 '14 11:04

user3515080


People also ask

How do you compare strings in Thymeleaf?

You can even compare String objects in thymeleaf. Under the hood, Thymeleaf uses the String. compareTo() method from the Comparable interface. That is, You can compare any two objects of the same type if they are Comparable .

How do you use condition in Thymeleaf?

Simple Conditionals: "if" and "unless"Thymeleaf provides th:if and th:unless conditional attributes to exclude elements based on a provided condition in the rendered document. Thymeleaf engine will evaluate the condition th:if="${user. active}" defined on the tr HTML element.

How do I get a request attribute in Thymeleaf?

Another way of accessing request parameters in thymeleaf is by using #httpServletRequest utility object which gives direct access to javax. servlet. http. HttpServletRequest object.

How do you check or condition in Thymeleaf?

To achieve similar to if-else condition in Thymeleaf we can use th:switch attribute. Thymeleaf at the first step will evaluate ${condition} expression and if the value is true it will print p tag with TRUE text. When an evaluated value is different than the true the Thymeleaf engine will generate p element with FALSE.


2 Answers

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>
like image 40
RiZKiT Avatar answered Sep 21 '22 16:09

RiZKiT


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>
    
like image 74
michal.kreuzman Avatar answered Sep 21 '22 16:09

michal.kreuzman