Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
806 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...