简体   繁体   English

不知道如何迭代<c:forEach>中提供的“items”

[英]Don't know how to iterate over supplied “items” in <c:forEach>

I am using JSF 1.2 in my application. 我在我的应用程序中使用JSF 1.2。 I am trying to iterate through a String array which is defined in my backing bean like this: 我试图遍历一个String数组,这个数组在我的支持bean中定义,如下所示:

private String[] services;

Below is the managed bean entry in faces-config file: 下面是faces-config文件中的托管bean条目:

<managed-bean>     
    <managed-bean-name>registrationBean</managed-bean-name>
    <managed-bean-class>com.bean.RegistrationBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>        
</managed-bean>

I am trying to iterate through the services selected by the user and display it in my screen. 我试图迭代用户选择的服务并将其显示在我的屏幕上。 Below is the code I used: 以下是我使用的代码:

<c:forEach items="#{registrationBean.services}" var="service">
    <c:out value="${service}"></c:out>
</c:forEach>

But I am getting the error: 但是我收到了错误:

Don't know how to iterate over supplied "items" in <c:forEach>

Kindly let me know how to resolve this. 请告诉我如何解决这个问题。

EDIT 编辑

If I change String[] to List<String> then I am getting this exception: 如果我将String[]更改为List<String>那么我将收到此异常:

java.lang.RuntimeException: wrapped Exception: java.lang.UnsupportedOperationException
com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet.java:156)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.icesoft.faces.webapp.xmlhttp.BlockingServlet.service(BlockingServlet.java:56)

Below is the Backing Bean initialization: 下面是Backing Bean初始化:

private List<String> services;
public RegistrationBean() {
    this.services = new ArrayList<String>();
}

Faces-config.xml: faces-config.xml中:

<?xml version='1.0' encoding='UTF-8'?>

<managed-bean>     
    <managed-bean-name>registrationBean</managed-bean-name>
    <managed-bean-class>com.bean.RegistrationBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>        
</managed-bean>   

<navigation-rule>
    <description>This will navigate to the Success screen.</description>
    <from-view-id>/registration.jspx</from-view-id>
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/success.jspx</to-view-id>
    </navigation-case>
</navigation-rule>

Web.xml: web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<description>
    ICEfaces Address Demo
</description>

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.application.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>

<context-param>
    <param-name>com.sun.faces.validateXml</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>com.icesoft.faces.synchronousUpdate</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
  <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
  <param-value>.jspx</param-value>
</context-param>

<listener>
    <listener-class>com.icesoft.faces.util.event.servlet.ContextEventRepeater</listener-class>
</listener>

<!-- Faces Servlet -->
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup> 1 </load-on-startup>
</servlet>

<servlet>
    <servlet-name>Persistent Faces Servlet</servlet-name>
    <servlet-class>com.icesoft.faces.webapp.xmlhttp.PersistentFacesServlet</servlet-class>
    <load-on-startup> 1 </load-on-startup>
</servlet>

<servlet>
    <servlet-name>Blocking Servlet</servlet-name>
    <servlet-class>com.icesoft.faces.webapp.xmlhttp.BlockingServlet</servlet-class>
    <load-on-startup> 1 </load-on-startup>
</servlet>

<!-- Faces Servlet Mapping -->
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jspx</url-pattern>
</servlet-mapping>

<!-- Persistent Faces Servlet Mapping -->
<servlet-mapping>
    <servlet-name>Persistent Faces Servlet</servlet-name>
    <url-pattern>*.iface</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>Persistent Faces Servlet</servlet-name>
    <url-pattern>/xmlhttp/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>Blocking Servlet</servlet-name>
    <url-pattern>/block/*</url-pattern>
</servlet-mapping>

<session-config>
  <session-timeout>30</session-timeout>
</session-config>


<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

I can't explain why you have this problem provided that you're on JSF 1.2. 如果您使用的是JSF 1.2,我无法解释为什么会出现此问题。 Most likely your project or environment is messed up with duplicate or conflicting libraries. 很可能您的项目或环境混乱了重复或冲突的库。 Or your faces-config.xml is declared conform JSF 1.1 instead of JSF 1.2. 或者你的faces-config.xml被声明为符合JSF 1.1而不是JSF 1.2。 Or perhaps ICEfaces played a role somehow, but I can't tell that as I've never really used that library. 或者也许ICEfaces以某种方式发挥了作用,但我无法分辨,因为我从未真正使用过该库。

In any case, I can explain specifically this problem when you're actually using JSF 1.1. 无论如何,当你实际使用JSF 1.1时,我可以特别解释这个问题。 The #{} is then not supported in JSP tags at all and the <c:forEach items> would produce exactly this error. 然后,JSP标记中根本不支持#{}<c:forEach items>会产生这个错误。

Don't know how to iterate over supplied "items" in <c:forEach> 不知道如何迭代<c:forEach>中提供的“items”

You should then be using ${} all the time in JSP tags. 然后,您应该始终在JSP标记中使用${} However, the ${} won't autocreate JSF managed beans when they are not present in the scope yet. 但是,当${}尚未在范围中出现时, ${}将不会自动创建JSF托管bean。 In that case, the <c:forEach> would effectively get an empty collection and render nothing (but thus not produce exactly the error you're facing!). 在这种情况下, <c:forEach>将有效地获得一个空集合并且不呈现任何内容(但因此不会产生您正面临的错误!)。

So, you'd need to make sure that the JSF managed bean is already been autocreated before the <c:forEach> is entered. 因此,您需要确保在输入<c:forEach>之前已经自动处理了JSF托管bean。 You can do that by using fullworthy JSF component like <h:panelGroup rendered="#{not empty bean.list}"> wrapping the tag, or a <h:outputText value="#{bean.text}"> before the tag. 您可以使用完整的JSF组件,如<h:panelGroup rendered="#{not empty bean.list}">包装标记,或者<h:outputText value="#{bean.text}">标签。

Eg 例如

<h:someComponent someAttribute="#{registrationBean.something}" />
<c:forEach items="${registrationBean.services}" var="service">
    <c:out value="${service}" />
</c:forEach>

A fullworthy JSF 1.x alternative would be to use Tomahawk's <t:dataList> instead of JSTL <c:forEach> . 一个完整的JSF 1.x替代方案是使用Tomahawk的 <t:dataList>而不是JSTL <c:forEach>

<t:dataList value="#{registrationBean.services}" var="service">
    <h:outputText value="#{service}" />
</t:dataList>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM