java – JSTL forEach标记:枚举问题,并了解它应该如何工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – JSTL forEach标记:枚举问题,并了解它应该如何工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3467字,纯文字阅读大概需要5分钟。
内容图文
![java – JSTL forEach标记:枚举问题,并了解它应该如何工作](/upload/InfoBanner/zyjiaocheng/797/0c5fea396aec4450867863acf2d90a99.jpg)
我经历过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}”更改为items =“${SessionBean.objects}”,并且没有例外.除了没有打印MyObjects.
然后,我对< h:outputText value =“#{myObject}”/>进行相同的更改,这是该属性的无效值.
最后,用${myObject}替换JSF outputText标记按预期工作.
有人可以解释一下,每个阶段会发生什么事吗?
U:SessionBean由JSF管理,肯定是创建的,因为它在头文件中执行一些操作.
解决方案:问题证明是由于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标记:枚举问题,并了解它应该如何工作全部内容,希望文章能够帮你解决java – JSTL forEach标记:枚举问题,并了解它应该如何工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。