【问题标题】:Understanding OGNL expressions in Struts2理解 Struts2 中的 OGNL 表达式
【发布时间】:2014-01-17 16:15:15
【问题描述】:

以下代码显示使用<s:a> 的Struts 从1 到10 的链接。

<s:set var="currentPage" value="2"/>
<s:set var="begin" value="1"/>
<s:set var="end" value="10"/>

<s:iterator begin="%{begin}" end="%{end}" step="1" var="row" status="loop">
    <s:if test="%{#currentPage eq #row}">    <!--???-->
        <span><s:property value="%{#row}"/></span>
    </s:if>

    <s:else>
        <s:url id="pageURL" action="someAction" escapeAmp="false">
            <s:param name="currentPage" value="%{row}"/>
        </s:url>

        <s:a href="%{pageURL}" name="btnPage" cssClass="paging">
            <s:property value="%{#row}"/>
        </s:a>
    </s:else>
</s:iterator>

currentPage(即2)与条件表达式test="%{#currentPage eq #row}" 匹配时,它只会在&lt;span&gt; 中使用&lt;s:property&gt; 显示文本,而不是显示链接。没关系。


当我使用这些相同的标签但在其相应的操作类中使用适当的属性时,

<s:iterator begin="%{begin}" end="%{end}" step="1" var="row" status="loop">
    <s:if test="%{currentPage eq #row}">   <!--???-->
        <span class="current"><s:property value="%{#row}"/></span>
    </s:if>

    <s:else>
        <s:url id="pageURL" action="someAction" escapeAmp="false">
            <s:param name="currentPage" value="%{row}"/>
        </s:url>

        <s:a href="%{pageURL}" name="btnPage" cssClass="paging">
            <s:property value="%{#row}"/>
        </s:a>
    </s:else>
</s:iterator>

在这种情况下,currentPage(和所有其他)是操作类中Long 类型的属性。在这里,关于前一个案例test="%{#currentPage eq #row}" 的条件测试被评估为false

需要在currentPage之前省略#。因此,表达式变为test="%{currentPage eq #row}"(否则,它总是评估为假)。

我不明白为什么第一种情况需要test="%{#currentPage eq #row}" 而第二种情况需要test="%{currentPage eq #row}"?有什么我可能会丢失的吗?

【问题讨论】:

    标签: jsp struts2 ognl valuestack


    【解决方案1】:

    当您&lt;s:set&gt; 一个值时,它不是 on 值堆栈,而是 in“值堆栈上下文”。

    使用裸currentPage 引用只搜索实际的堆栈,而不是上下文。

    使用#currentPage 不会检查堆栈本身,而是引用堆栈上下文

    还可以查看这些其他答案:

    1. what's the difference between #{} ${} and %{}?
    2. Struts 2 "%" sign and '#" sign in OGNL

    【讨论】:

    • 所有动作对象/变量是否都存储在ActionContext下的值栈中?
    • @Tiny 一旦你点击视图层,动作本身就在堆栈的顶部(通常),尽管你可以将自己的值放在它上面,例如,使用&lt;s:push&gt; 或 @ 987654328@等
    • Using the bare currentPage reference only searches the actual stack, not the value stack context 不正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多