比较Struts2中的字符串< s:if>标签 [英] Comparing Strings in Struts2 <s:if> Tag
问题描述
我有一个index.jsp
页面,其中某些元素根据用户是否登录而打开/关闭.
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>
accessType
设置为GUEST
.但是,即使用户尚未登录,它也会进入else-block.
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?
更新:
我从index.jsp中删除了会话部分,只是看到了,现在看起来像这样:
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>
问题:
- 条件进入
<s:else>
块. - 访问类型未打印(以某种方式未设置).
推荐答案
您正在比较s:if
语句中的字符串,并且在变量中使用字符串值时,它在测试文件中工作良好.因此,"'GUEST'"
起作用了.没有引号的OGNL尝试将值解析为双引号,如果在值堆栈中找不到名为GUEST
的变量,它将返回null
.然后,您尝试打印此值,该值将转换为空字符串.您还已经在JSP中对'GUEST'
的值进行了硬编码,这不是很好.
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.
解决方案:您不应该在JSP中编写的业务逻辑.您应该在操作中定义会话变量,并在JSP中访问该会话变量,而不是根据某些逻辑条件重新定义变量值.
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>
这篇关于比较Struts2中的字符串< s:if>标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!