2

在我的页面中,<ui:repeat>不起作用。<c:forEach>是工作。我不知道我错过了什么?<ui:repeat>否则, JSF 2.0不工作?

mypage.xhtml(它不起作用)

<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(没关系)

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

日期选择.java

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

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

输出将在我的页面中如下所示。

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

1 回答 1

0

我使用了 Java EE 6 注释,它在 GlassFish 3.1.2.2 和 Mojarra 2.1.6 上运行良好。我确实修改了您的代码,但在这里。

@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;
    }
}

这是页面...

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

结果是预期的输出。注意:我没有开始或结束对话。

有一些问题在 2.1 中在几个不同的版本中得到了修复。您需要查看特定版本的发行说明和已解决的问题。

更新

我在这里发布了完整的解决方案:stackoverflow-ui-repeat-example

于 2012-12-10T16:30:45.617 回答