如何使用JSTL< c:forEach>与Struts2< s:url>? [英] How to use JSTL <c:forEach> with Struts2 <s:url>?
问题描述
我的代码实际上有效:
<s:iterator value="breadcrumb.links" var="link">
<s:url action='%{#link.url}' var="url" />
<li>
<a href="${url}">${link.name}</a>
</li>
</s:iterator>
我怎么做同样的事情,但用c:foreach而不是s:iterator?
How con I do the same thing but with c:foreach instead of s:iterator ?
我试过:
<c:forEach items="${breadcrumb.links}" var="link">
<s:url action='${link.url}' var="url" />
<li>
<a href="${url}">${link.name}</a>
</li>
</c:forEach>
但我收到错误:
根据标签文件中的TLD或属性指令,属性操作不接受任何表达式
According to TLD or attribute directive in tag file, attribute action does not accept any expressions
谢谢你。
推荐答案
为了更加熟悉Struts2标签和OGNL语言,读取并为此答案添加书签。
To be more comfortable with Struts2 tags and OGNL language, read and bookmark this answer.
由于Struts2标记仅评估OGNL表达式(而不是EL表达式 ,当您错误明确指出)时,您需要通过使用 var <指定的PageContext属性(在OGNL
#attr.something
中)访问JSTL对象/ code>:
Since Struts2 tags only evaluate OGNL expressions (and not EL Expression, as you error clearly states), you need to access the JSTL object through the PageContext attribute (in OGNL #attr.something
) specified with var
:
<c:forEach items="${breadcrumb.links}" var="link">
<s:url action='%{#attr.link.url}' var="url" />
<li>
<a href="${url}">${link.name}</a>
</li>
</c:forEach>
来自 OGNL基础知识:
#attr ['foo']
或#attr.foo
:访问PageContext
如果可用,否则搜索
request
/session
/application
分别
#attr['foo']
or#attr.foo
: Access toPageContext
if available, otherwise searchesrequest
/session
/application
respectively
这篇关于如何使用JSTL< c:forEach>与Struts2< s:url>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!