How do I use multiple conditions in thymleaf - "If statement"

159 Views Asked by At

I am trying to display a delete button only for the person who post a comment and for the ADMIN, however when I try to hide it using an if statment or sec:authorize there is an error parsing the template. I tried to do two separete buttons using each if condition separately and it worked fine, however when I combine them using OR there is a mistake.

This is what I have wrote so far. // the HTML

            <a th:if="${c.email}==${namePrincipal} or hasRole('ADMIN')" href="#" class="btn btn-secondary" style="background-color: #8db8e0; margin-left: 200px;">Delete</a>
       

The error message: org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/show-comments.html]")

Caused by: org.attoparser.ParseException: Could not parse as expression: "${c.email}==${namePrincipal} or hasRole('ADMIN')" (template: "show-comments" - line 34, col 20)

1

There are 1 best solutions below

0
Surya mdl On

To give the permission based on roles ,sec:authorize is used

       <div sec:authorize="hasRole('ROLE_ADMIN')">
                    <div th:if="${#authentication.principal.username} eq ${c.getEmail()}">


            <a href="#" class="btn btn-secondary" style="background-color: #8db8e0; margin-left: 200px;">Delete</a>

                    </div>
                </div>