gpt4 book ai didi

java - JSTL forEach 标记 : problems with enumeration, 并了解它应该如何工作

转载 作者:搜寻专家 更新时间:2023-11-01 02:35:03 24 4
gpt4 key购买 nike

我经历过 JSTL forEach 标签的奇怪行为。

我有一些名为 SessionBean 的 bean:

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

还有一个像这样的简单 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>

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

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)   ....

为什么?
然后我改变items="#{SessionBean.objects}"到项目= "${SessionBean.objects}" ,也不异常(exception)。 MyObjects 除外。

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

最后,替换 JSF outputText标签只有 ${myObject}按预期工作。

请问有人可以解释一下每个阶段发生了什么吗?

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

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

谢谢!

最佳答案

article解释统一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.

<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.

我想知道问题是不是因为没有创建 SessionBean 的实例?

试试这个:

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

[更新]不知这个article可能会有帮助。它解释了两个 EL 如何协同工作。

关于java - JSTL forEach 标记 : problems with enumeration, 并了解它应该如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/256910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com