java 比较 Struts2 <s:if> 标签中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27676055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:12:45  来源:igfitidea点击:

Comparing Strings in Struts2 <s:if> Tag

javajspstruts2ognl

提问by silver

I have an index.jsppage wherein certain elements turn on/off depending on whether the user has logged in or not.

我有一个index.jsp页面,其中某些元素根据用户是否登录而打开/关闭。

<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 accessTypeis set to GUEST. However, it's entering the else-block even if the user has not logged in yet.

accessType设置为GUEST。但是,即使用户尚未登录,它也会进入 else 块。

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:

更新:
我从 index.jsp 中删除了 session 部分只是为了查看,现在看起来像这样:

<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:

问题:

  1. Condition enters <s:else>block.
  2. Access type is not printing (somehow not being set).
  1. 条件进入<s:else>阻塞。
  2. 访问类型未打印(不知何故未设置)。

采纳答案by Roman C

You are comparing a string in the s:ifstatements 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 GUESTin 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.

您正在比较s:if语句中的字符串,当您为变量使用字符串值时,它在测试文件中运行良好。所以,"'GUEST'"工作了。没有引号 OGNL 试图解析双引号中的值,如果它找不到GUEST值堆栈中命名的变量,则返回null。然后您尝试打印此转换为空字符串的值。您还在'GUEST'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.

解决方案:您不应该用 JSP 编写业务逻辑。您应该在操作中定义会话变量并在 JSP 内部访问此会话变量,而不是根据某些逻辑条件重新定义变量值。

<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>

回答by silver

I have found the culprit.

我找到了罪魁祸首。

To set String value in Struts2 var, this line:

要在 Struts2 var 中设置 String 值,这一行:

<s:set var="accessType" value="GUEST" />

<s:set var="accessType" value="GUEST" />

Should be:

应该:

<s:set var="accessType" value="'GUEST'" />

<s:set var="accessType" value="'GUEST'" />

Single quotes (' ') must surround the String literal when placed inside the value=""attribute.

单引号 (' ') 在放置在value=""属性内时必须将字符串文字括起来。

The if-check was correct.

如果检查是正确的。