如何使用 JSTL <c:forEach>使用 Struts2 <s:url>? [英] How to use JSTL <c:forEach> with Struts2 <s:url>?

查看:39
本文介绍了如何使用 JSTL <c:forEach>使用 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>

我如何做同样的事情,但使用 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 表达式,正如您清楚地指出的那样),您需要通过 PageContext 属性访问 JSTL 对象(在 OGNL #attr.something) 用 var 指定:

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 to PageContext if available, otherwise searches request/session/application respectively

这篇关于如何使用 JSTL &lt;c:forEach&gt;使用 Struts2 &lt;s:url&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆