I am using JSF 1.2 in my application. I am trying to iterate through a String ...">

I donโ€™t know how to iterate over delivered "objects" in <c: forEach>

I am using JSF 1.2 in my application. I am trying to iterate through a String array, which is defined in my bean support as follows:

private String[] services; 

The following is a managed bean entry in the faces-config file:

 <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 them on 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 get an error message:

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

Please let me know how to solve this.

EDIT

If I change String[] to List<String> , I get this exception:

 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 bean support:

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

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:

 <?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> 
+4
source share
1 answer

I can not explain why you have this problem, provided that you use JSF 1.2. Most likely your project or environment is confused with duplicate or conflicting libraries. Or your faces-config.xml declared as JSF 1.1 instead of JSF 1.2. Or perhaps ICEfaces played a role in some way, but I cannot say that since I have never used this library.

In any case, I can specifically explain this problem when you use JSF 1.1. #{} then is not supported at all in JSP tags, and <c:forEach items> will produce exactly this error.

I donโ€™t know how to iterate over the supplied "elements" in <c: forEach>

Then you should use ${} all the time in JSP tags. However, ${} will not automatically create JSF managed beans if they are not already in the scope. In this case, <c:forEach> will effectively receive an empty collection and not display anything (but, thus, does not create exactly the error that you encountered!).

So, you need to make sure that the managed JSF bean has already been auto-registered before entering <c:forEach> . You can do this using a full-featured JSF component, for example, <h:panelGroup rendered="#{not empty bean.list}"> , a wrapping tag, or <h:outputText value="#{bean.text}"> before tag.

eg.

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

A complete alternative to JSF 1.x would be to use Tomahawk <t:dataList> instead of JSTL <c:forEach> .

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

All Articles