java JSTL forEach 标签:枚举问题,以及理解它应该如何工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/256910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:39:29  来源:igfitidea点击:

JSTL forEach tag: problems with enumeration, and with understanding how it should work

javajspjstl

提问by ansgri

I've experienced rather strange behavior of JSTL forEach tag.

我经历过 JSTL forEach 标记的相当奇怪的行为。

I have some bean called SessionBean:

我有一些名为 SessionBean 的 bean:

public class SessionBean {
  private Collection<MyObject> objects;
  public Collection<MyObject> getObjects() {return objects;}
  ...
}

And a simple JSP page like that:

还有一个像这样的简单 JSP 页面:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
    <f:view>
        <h:form>
            <c:forEach var="myObject" items="#{SessionBean.objects}">
                <h:outputText value="#{myObject}" /> <br />
            </c:forEach>
        </h:form>
    </f:view>
</body>

And, it doesn't work. Exeption thrown is

而且,它不起作用。抛出的异常是

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
        at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:255)
        at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:219)
   ....

Why?
And then I change items="#{SessionBean.objects}"to items="${SessionBean.objects}", and there's no exception. Except for MyObjects aren't printed.

为什么?
然后我更改items="#{SessionBean.objects}"为 items= "${SessionBean.objects}",也不例外。除了不打印 MyObjects。

Then, I make the same change to <h:outputText value="#{myObject}" />, and it's invalid value for this attribute.

然后,我对 进行了相同的更改<h:outputText value="#{myObject}" />,该属性的值无效。

Finally, replacing JSF outputTexttag with just ${myObject}works as expected.

最后,将 JSFoutputText标记替换${myObject}为按预期工作。

Could somebody explain, what happens here on each phase, please?

有人可以解释一下,每个阶段会发生什么吗?

U:SessionBean is managed by JSF, and surely was created, for it performs some actions in the header.

U:SessionBean 由JSF 管理,肯定是创建的,因为它在头中执行一些操作。

RESOLUTION:The problem proved to be due to incompatibility between JSTL and JSF in J2EE 1.4. Switching to J2EE 5 made the first variant work just fine.

解决方案:该问题被证明是由于 J2EE 1.4 中 JSTL 和 JSF 之间的不兼容造成的。切换到 J2EE 5 使第一个变体工作得很好。

Thanks!

谢谢!

回答by Vincent Ramdhanie

This articleexplains the difference between the unified EL and the EL. Here is a snippet

文章解释了统一EL和EL之间的差异。这是一个片段

Evaluation of EL

Evaluation of EL is categorized as immediate evaluation and deferred evaluation. Immediate evaluation means a JSP page evaluates the expression when the page is rendered. With immediate evaluation, all values are always read-only. JSP EL expressions take the form of ${imExpr}. JSP expressions are evaluated immediately.

Deferred evaluation means that the technology using the unified EL takes over the responsibility of evaluating the expression from the JSP engine and evaluates the expression at the appropriate time during the page lifecycle. The EL takes control from the JSP container to evaluate the expression at the appropriate time. JSF EL expressions take the form of #{defExpr}. JSF expressions work in this way.

The following example shows a JSF inputText tag, which represents a text field component into which a user enters a value. The inputText tag's value attribute references an expression that points to the name property of the book bean.

EL的评价

EL的评估分为即时评估和延期评估。立即评估意味着 JSP 页面在呈现页面时评估表达式。通过立即评估,所有值始终为只读。JSP EL 表达式采用 ${imExpr} 的形式。JSP 表达式会立即求值。

延迟评估是指使用统一 EL 的技术从 JSP 引擎接管评估表达式的责任,并在页面生命周期中的适当时间评估表达式。EL 从 JSP 容器中取得控制权,以在适当的时间评估表达式。JSF EL 表达式采用#{defExpr} 的形式。JSF 表达式以这种方式工作。

以下示例显示了一个 JSF inputText 标记,它表示用户在其中输入值的文本字段组件。inputText 标记的 value 属性引用一个表达式,该表达式指向 book bean 的 name 属性。

<h:inputText id="name" value="#{student.name}"/>

For an initial request of the page containing this tag, the JSF implementation evaluates the #{student.name} expression during the "render response" phase of the lifecycle. During this phase, the expression merely accesses the value of quantity from the book bean, as is done in immediate evaluation.

For a postback, the implementation evaluates the expression during the "apply request values," "process validations," and "update model" phases, during which the value is retrieved from the request, validated, and propagated to the book bean.

对于包含此标记的页面的初始请求,JSF 实现在生命周期的“呈现响应”阶段评估 #{student.name} 表达式。在此阶段,表达式仅访问 book bean 中的数量值,就像在立即评估中所做的那样。

对于回发,实现在“应用请求值”、“过程验证”和“更新模型”阶段评估表达式,在此期间,值从请求中检索、验证并传播到图书 bean。

I am wondering if the problem is from the fact that an instance of SessionBean was not created?

我想知道问题是否来自未创建 SessionBean 实例的事实?

Try this:

试试这个:

<jsp:useBean class="packagename.SessionBean" id="sb"/>
<c:forEach var="myObject" items="${sb.objects}">
            <h:outputText value="${myObject}" /> <br />
</c:forEach>

[Update] I wonder if this articlemight help then. It explains how the two EL's work together.

[更新] 我想知道这篇文章可能会有所帮助。它解释了两个 EL 如何协同工作。