了解Struts2中的OGNL表达式 [英] Understanding OGNL expressions in Struts2
问题描述
以下代码使用Struts的<s:a>
从1到10显示链接.
The following code displays links using <s:a>
of Struts starting from 1 to 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}"
匹配时,它仅在<span>
内使用<s:property>
显示文本,而不显示链接.很好.
When currentPage
(which is 2) matches the conditional expression test="%{#currentPage eq #row}"
, it just displays text using <s:property>
inside <span>
instead of showing a link. That's fine.
当我使用这些相同的标签但在其相应的动作类中使用适当的属性时,
When I use these same tags but using appropriate properties in its corresponding action class like so,
<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 .
In this case, currentPage
(and all other) is a property of type Long
in the action class. Here, the conditional test regarding the previous case which is test="%{#currentPage eq #row}"
is evaluated to false.
它要求在currentPage
之前省略#
.因此,表达式变为test="%{currentPage eq #row}"
(否则,始终的计算结果为false).
It requires the omission of #
before currentPage
. Hence, the expression becomes test="%{currentPage eq #row}"
(otherwise, it always evaluates to false).
我不明白为什么第一种情况要求test="%{#currentPage eq #row}"
而第二种情况要求test="%{currentPage eq #row}"
?我可能有什么想念的吗?
I don't understand why does the first case require test="%{#currentPage eq #row}"
and the second case require test="%{currentPage eq #row}"
? Is there anything I might be missing?
推荐答案
当您<s:set>
值时,它不在 值堆栈中,而是 in 中的值堆栈上下文".
When you <s:set>
a value it's not on the value stack, but instead in the "value stack context".
使用裸currentPage
引用仅搜索实际的堆栈,而不是上下文.
Using the bare currentPage
reference only searches the actual stack, not the context.
使用#currentPage
不会检查堆栈本身,而是引用堆栈 context .
Using #currentPage
doesn't check the stack itself, but instead references the stack context.
也请参见以下其他答案:
See these other answers as well:
- what's the difference between #{} ${} and %{}?
- Struts 2 "%" sign and '#" sign in OGNL
这篇关于了解Struts2中的OGNL表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!