【问题标题】:JSF 2 custom component with h:commandLink inside ui:repeat带有 h:commandLink 的 JSF 2 自定义组件在 ui:repeat 中
【发布时间】:2012-01-29 04:03:54
【问题描述】:

我开发了一个自定义组件(灵感来自 this articleBalusC),其中包含一个显示可点击表格页面列表的 ui:repeat 标签。
在循环块内有一个h:commandLink,它异步调用一个动作,发布一个参数。参数值在每个循环中都会发生变化。这是页面代码:

[...]
<ui:repeat value="#{cc.attrs.pager.pages}" var="loopPage">
    <li>
        <h:commandLink 
            action="#{cc.attrs.pager.changePage}"
            value="#{loopPage}"
            rendered="#{loopPage != cc.attrs.pager.currentPage}"
            immediate="true"
        >
            <f:param name="selectedPage" value="#{loopPage}" />
            <f:ajax execute="@this" render="#{cc.attrs.render}" />
        </h:commandLink>

        <h:outputText 
            value="#{loopPage}" 
            rendered="#{loopPage == cc.attrs.pager.currentPage}"
        />
    </li>
</ui:repeat>
[...]

现在,cc.attrs.pager 组件属性注意到视图范围的支持 bean,其 pagescurrentPage 属性通过 getter 可见,changePagemethod 是要调用的操作。 cc.attrs.render 组件属性是每次操作执行后要刷新的目标。这是相关的bean代码:

[...]

private Integer[] pages;
private int currentPage;

[...]

public Integer[] getPages() {
    return pages;
}

public int getCurrentPage() {
    return currentPage;
}

[...]

public void changePage() {
    HttpServletRequest request = (HttpServletRequest) FacesContext
        .getCurrentInstance()
        .getExternalContext()
        .getRequest()
    ;

    String page = request.getParameter("selectedPage");
    changePage(Integer.valueOf(page) * rowsPerPage);
}

public void changePage(int firstRow){
    this.firstRow = firstRow; //Set the first result to be returned
    loadDataList(); // Load requested page.
}

[...]

初始渲染是完美的:所有页面链接都正确显示,并且当前页码未按需要链接。问题显示转到另一个页面:没有任何反应。
尽管h:commandLink 操作与确切的循环参数值一起发布,但响应总是返回相同的页面(第一个)。 调试它,我观察到执行没有进入两个changePage 方法中的任何一个。 我不确定这个问题是否与ui:repeat 有关,但忽略它,我可以看出相同的机制确实适用于其他目的。

有什么建议吗?

【问题讨论】:

    标签: jsf-2 foreach composite-component mojarra uirepeat


    【解决方案1】:

    我认为您应该尝试将&lt;ui:repeat&gt; 替换为&lt;c:forEach&gt;

    【讨论】:

    • 感谢您的回复。你能解释一下我为什么要这样做吗?
    • 有效吗?如果您使用的是 Mojarra,&lt;ui:repeat&gt; 有问题。你不应该使用它。
    • 实际上,我使用的是 Mojarra 2.0.6。错误应该是this one。您提供的解决方案效果很好。感谢您的支持!
    • 另一个问题:在cc.attrs.pager.pages 上使用c:forEach,第一页渲染没有显示任何内容。我认为这是因为c:forEach 评估时集合尚未初始化。对吗?
    • Mojarra 2.1.6 中似乎也存在该问题
    猜你喜欢
    • 2015-01-19
    • 2014-02-21
    • 1970-01-01
    • 2013-03-19
    • 2012-03-30
    • 2015-01-02
    • 2015-12-01
    • 2012-03-29
    • 2013-08-23
    相关资源
    最近更新 更多