w_retors
w_retors

Reputation: 35

Using variables in jstl if statement

I have something like this

 <c:forEach var="authority" items="#{SPRING_SECURITY_CONTEXT.authentication.principal.authorities}">

      <c:if test="${fn:contains( ${authority} , ROLE_ADMIN) }">

      </c:if>
</c:forEach>

but it does not working, any ide how can I make this? Maybe there is some other way?

//Edited

Upvotes: 1

Views: 1918

Answers (2)

BalusC
BalusC

Reputation: 1109645

There are 2 problems:

  1. You should not nest EL expressions. Just reference the EL variable the usual way.

    <c:if test="${fn:contains(authority, ROLE_ADMIN)}">
    
  2. It's not clear from the question context if ROLE_ADMIN is already in the EL scope. But you need to know that you cannot reference constants in EL. If it's for example an enum, then it's good to know that they are just evaluated as String. You need to quote it then.

    <c:if test="${fn:contains(authority, 'ROLE_ADMIN')}">
    

Upvotes: 1

Mark Pope
Mark Pope

Reputation: 11274

You're missing a closing ")" in your if test:

 <c:forEach var="authority" items="#{SPRING_SECURITY_CONTEXT.authentication.principal.authorities}">

      <c:if test="${fn:contains( {$authority} , ROLE_ADMIN ) }">

      </c:if>
</c:forEach>

Upvotes: 0

Related Questions