1

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

<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(which is 2) 匹配条件表达式test="%{#currentPage eq #row}"时,它只使用<s:property>inside显示文本,<span>而不是显示链接。没关系。


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

<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

它需要省略#before currentPage。因此,表达式变为test="%{currentPage eq #row}"(否则,它总是计算为假)。

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

4

1 回答 1

2

当你<s:set>一个值时,它不在值堆栈,而是“值堆栈上下文”中。

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

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

另请参阅这些其他答案:

  1. #{} ${} 和 %{} 有什么区别?
  2. OGNL中的Struts 2“%”符号和“#”符号
于 2013-12-30T18:46:23.133 回答