比较Struts2中的字符串< s:if>标签 [英] Comparing Strings in Struts2 <s:if> Tag

查看:65
本文介绍了比较Struts2中的字符串< s:if>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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>

问题:

  1. 条件进入<s:else>块.
  2. 访问类型未打印(以某种方式未设置).

推荐答案

您正在比较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中的字符串&lt; s:if&gt;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆