问题
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:
- Condition enters
<s:else>block. - Access type is not printing (somehow not being set).
回答1:
You are comparing a string in the s:if statements and it worked good in the test file when you used a string value for the variable. So, "'GUEST'" worked. Without quotes OGNL is trying to parse the value in double quotes and if it can't find a variable named GUEST in the value stack it returns null. Then you tried to print this value that is converted to empty string. You have also hardcoded the value of 'GUEST' in the JSP, it's not good.
Solution: business logic you shouldn't write in JSP. You should define the session variable in the action and access this session variable inside JSP instead of redefining a variable value based on some logical condition.
<head>
<!--
<s:set var="accessType" value="GUEST" />
removed session code for testing -->
</head>
<body>
<nav>
<s:if test="#session.currentAccessType.equals('GUEST')">
<ul>
<li><a href="index.jsp">Home</a></li>
<li><a href="#">Login</a></li>
<li><a href="http://www.iacademy.edu.ph" 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.iacademy.edu.ph" target="_blank">Main Site</a></li>
</ul>
</s:else>
</nav>
<br />
Access type is <s:property value="#session.currentAccessType" />.
</body>
回答2:
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.
来源:https://stackoverflow.com/questions/27676055/comparing-strings-in-struts2-sif-tag