简体   繁体   中英

ui:repeat does not work in JSF 2.0?

In my page, <ui:repeat> does not work. <c:forEach> is work. I don't know what am I missing? Otherwise, Does not <ui:repeat> work JSF 2.0?

mypage.xhtml (it does not work)

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html" 
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                template="/common/commonLayout.xhtml">
    <ui:define name="content">
        <h:form id="toDeleteForm">
                <table>
                    <tr>
                        <td>
                            <ui:repeat value="#{DatePick.timeSlot}" var="timeSlot">
                                <h:outputText value="#{timeSlot}" style="font-size:12px;"/><br/>
                            </ui:repeat>
                        </td>
                    </tr>
                </table>
        </h:form>
    </ui:define>
</ui:composition>

mypage.xhtml (it is ok)

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html" 
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                template="/common/commonLayout.xhtml">
    <ui:define name="content">
        <h:form id="toDeleteForm">
                <table>
                    <tr>
                        <td>
                            <c:forEach items="#{DatePick.timeSlot}" var="timeSlot">
                                <h:outputText value="#{timeSlot}" style="font-size:12px;"/>
                            </c:forEach>
                        </td>
                    </tr>
                </table>
        </h:form>
    </ui:define>
</ui:composition>

DatePick.java

@Name("DatePick")
@Scope(ScopeType.CONVERSATION)
public class DatePick {

    public List<String> getTimeSlot() {
        // list form database
        return timeSlot;
    }
}

Output will be as below in my page.

    01/01/2012
02/01/2012
03/01/2012
04/01/2012
05/01/2012

I used Java EE 6 annotations and it worked fine on GlassFish 3.1.2.2 with Mojarra 2.1.6. I did modify your code, but here it is.

@Named
@ConversationScoped
public class DatePick implements Serializable {

    private static final long serialVersionUID = -5061581851476260511L;

    public DatePick() {
    }

    public List<String> getTimeSlot() {
        List<String> dates = new ArrayList<String>() {
            private static final long serialVersionUID = 3109256773218160485L;

            {
                add("01/1/2012");
                add("02/1/2012");
                add("03/1/2012");
                add("04/1/2012");
                add("05/1/2012");
                add("06/1/2012");
                add("07/1/2012");
                add("08/1/2012");
                add("09/1/2012");
                add("10/1/2012");
                add("11/1/2012");
                add("12/1/2012");
            }
        };
        return dates;
    }
}

and here is the page...

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./WEB-INF/templates/main.xhtml"
                xmlns:h="http://java.sun.com/jsf/html">
    <ui:define name="content">
        <table>
            <tr>
                <td>
                    <ui:repeat value="#{datePick.timeSlot}" var="timeSlot">
                        <h:outputText value="#{timeSlot}"/><br/>
                    </ui:repeat>
                </td>
            </tr>
        </table>
    </ui:define>
</ui:composition>

The result was the expected output. Note: I did not start, or end a conversation.

There were some issues with that were fixed in 2.1 in a couple of different builds. You would need to look at the release notes for a particular build and issues resolved.

UPDATE

I posted the complete solution here: stackoverflow-ui-repeat-example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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