I have an index.jsp
page wherein certain elements turn on/off depending on whether the user has logged in or not.
<head>
<s:set var="accessType" value="GUEST" />
<s:if test="#session.containsKey('currentAccessType')">
<s:set var="accessType" value="#session.currentAccessType" />
</s:if>
</head>
<body>
<nav>
<s:if test="#accessType.equals('GUEST')">
<ul>
<li><a href="index.jsp">Home</a></li>
<li><a href="#">Login</a></li>
<li><a href="http://www.mywebsite.com" target="_blank">Main Site</a></li>
</ul>
</s:if>
<s:else>
<ul>
<li><a href="index.jsp">Home</a></li>
<li><a href="#">Control Panel</a></li>
<li><a href="#">Logout</a></li>
<li><a href="http://www.mywebsite.com" target="_blank">Main Site</a></li>
</ul>
</s:else>
</nav>
</body>
The accessType
is set to GUEST
. However, it's entering the else-block even if the user has not logged in yet.
Did I commit an error in my String comparison?
UPDATE:
I removed the session part from the index.jsp just to see and it now looks like this:
<head>
<s:set var="accessType" value="GUEST" />
<!-- removed session code for testing -->
</head>
<body>
<nav>
<s:if test="#accessType.equals('GUEST')">
<ul>
<li><a href="index.jsp">Home</a></li>
<li><a href="#">Login</a></li>
<li><a href="http://www.mywebsite.com" target="_blank">Main Site</a></li>
</ul>
</s:if>
<s:else>
<ul>
<li><a href="index.jsp">Home</a></li>
<li><a href="#">Control Panel</a></li>
<li><a href="#">Logout</a></li>
<li><a href="http://www.mywebsite.com" target="_blank">Main Site</a></li>
</ul>
</s:else>
</nav>
<br />
Access type is <s:property value="#accessType" />.
</body>
Problems:
<s:else>
block.I have found the culprit.
To set String value in Struts2 var, this line:
<s:set var="accessType" value="GUEST" />
Should be:
<s:set var="accessType" value="'GUEST'" />
Single quotes (' ') must surround the String literal when placed inside the value=""
attribute.
The if-check was correct.