Search code examples
jspjstl

JSTL c:if tag does not seem to have any effect


I've got the following code I am trying to switch from scriptlet:

    <% if (session.getAttribute("korisnik") == null) { %>
    <a href='login.jsp'>Ulogujte se</a>
    <a href='registracija.jsp'>Registrujte se</a>
    <% } %>

to el:

    <c:if test="${sessionScope.korisnik == null}">
    <a href='login.jsp'>Ulogujte se2</a>
    <a href='registracija.jsp'>Registrujte se2</a>
    </c:if>

The scriptlet code works (hides the login and register pages if user is logged in aka has data in session) but I heard that it's bad practice so am trying to switch it to EL, but the EL part doesn't work, and I tried countless variations (just korisnik, with session scope, comparing to 0 etc).

I'm sure I'm missing something incredibly obvious here, any help would be appreciated.


Solution

  • Remember to import the taglib like so:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    

    Then this should work:

    <c:if test="${sessionScope.korisnik == null}">
      <a href='login.jsp'>Ulogujte se2</a>
      <a href='registracija.jsp'>Registrujte se2</a>
    </c:if>
    

    Make sure the taglib is above the <html> tag