How to use JSTL <c:forEach> with Struts2 <s:url>?

喜夏-厌秋 提交于 2019-12-18 06:57:08

问题


I have this code that actually works:

<s:iterator value="breadcrumb.links" var="link">
    <s:url action='%{#link.url}' var="url" />
    <li>
       <a href="${url}">${link.name}</a>
    </li>
</s:iterator>

How con I do the same thing but with c:foreach instead of s:iterator ?

I tried with:

<c:forEach items="${breadcrumb.links}" var="link">
    <s:url action='${link.url}' var="url" />
    <li>
        <a href="${url}">${link.name}</a>
    </li>
</c:forEach>

but I get the error:

According to TLD or attribute directive in tag file, attribute action does not accept any expressions

Thankyou.


回答1:


To be more comfortable with Struts2 tags and OGNL language, read and bookmark this answer.

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>

From OGNL Basics:

#attr['foo'] or #attr.foo : Access to PageContext if available, otherwise searches request/session/application respectively



来源:https://stackoverflow.com/questions/20457431/how-to-use-jstl-cforeach-with-struts2-surl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!