4

I am using JSF 1.2 in my application. I am trying to iterate through a String array which is defined in my backing bean like this:

private String[] services;

Below is the managed bean entry in 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 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:

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:

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>
user182944
  • 7,897
  • 33
  • 108
  • 174
  • Try changing `String[]` for `List`. – Luiggi Mendoza Apr 10 '13 at 15:58
  • I tried but got an exception as mentioned in the Edit section of my first post. Any further help on this? – user182944 Apr 10 '13 at 16:03
  • Are you sure your managed bean is created, the `List service` is initialized in your bean constructor or in `@PostConstruct` method and is not empty and the getter is clean: `public List getService() { return this.service; }`? – Luiggi Mendoza Apr 10 '13 at 16:06
  • Yes, I have provided the backing bean initialization constructor as well as the Managed bean in the first post. Please check and let me know. – user182944 Apr 10 '13 at 16:11
  • Are you really using JSF 1.2? This problem suggests that you're actually using JSF 1.1, or are running JSF 1.2 in JSF 1.1 fallback modus. The `#{}` is then not supported in JSP tags. Please list the JARs in `/WEB-INF/lib` and show the `web.xml` and `faces-config.xml` root declarations so that we can point out the culprit. the `UnsupportedOperationException` is by the way beyond me. This indicates rather classpath pollution with duplicate or incompatible library versions (which would also potentially explain your JSTL problem though) – BalusC Apr 10 '13 at 16:13
  • Ok the jars used are :`jsf-api-1.2.jar` and `jsf-impl-1.2.jar`. I have `icefaces-comps.jar` and `icefaces.jar` also added but still I have not used any icefaces stuff in this project. Along with this I have jstl.jar and standard.jar added. Others are all commons related jar need by the Tomcat and `backport-util-concurrent-2.2,jar` – user182944 Apr 10 '13 at 16:18
  • posted the web.xml and faces-config.xml in first post – user182944 Apr 10 '13 at 16:22
  • If the problem is in your `index.jsp` file is because isn't matching with Faces Servlet mapping: `*.jspx`, `*.faces`. – Luiggi Mendoza Apr 10 '13 at 16:24
  • I have this in my index.jsp: `` which redirects me to the registration.jspx file. – user182944 Apr 10 '13 at 16:27
  • Are you sure the second problem is not a different problem that the one stated at the beginning? – Luiggi Mendoza Apr 10 '13 at 16:30
  • Begining problem was for using `String[]`. Then based on your suggestion changed it to `List` and got the problem mention in the edit section of the first post. – user182944 Apr 10 '13 at 16:31
  • I mean that first problem may be solved with the suggestion but now you have this second problem due to something else that is not posted here. It will be better if you have a clean page where you only try your `` tag with the managed bean. If there's no problems there, start adding the JSF and IceFaces components to find the real problem. – Luiggi Mendoza Apr 10 '13 at 16:44

1 Answers1

5

I can't explain why you have this problem provided that you're on 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. Or perhaps ICEfaces played a role somehow, but I can't tell that as I've never really used that library.

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

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

You should then be using ${} all the time in JSP tags. However, the ${} won't autocreate JSF managed beans when they are not present in the scope yet. 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!).

So, you'd need to make sure that the JSF managed bean is already been autocreated before the <c:forEach> is entered. 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.

E.g.

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

<t:dataList value="#{registrationBean.services}" var="service">
    <h:outputText value="#{service}" />
</t:dataList>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555