RichFaces tabPanel,带有switchType ajax和a4j:status

时间:2012-10-01 13:11:44

标签: jsf-2 richfaces ajax4jsf

我试图实现的目标

我有一个rich:tabPanel switchType="client",其中一个标签已将其switchType设置为ajax,因为加载其数据是一项昂贵的操作。当我点击此 Ajax选项卡时,我希望将其标题更改为正在加载... 以指示已识别出该点击。加载完成后,标题应切换回 Ajax选项卡

[Client Tab] [Ajax Tab]
<click on 'Ajax Tab'>
[Client Tab] [Loading...]
<wait...>
[Client Tab] [Ajax Tab]
<click on 'Client Tab'>
[Client Tab] [Ajax Tab]

问题描述

实际上,上面描述的大多数都按预期工作,我唯一的问题是,当我从 Ajax选项卡导航到任何其他选项卡时, Ajax选项卡的标题切换回正在加载...

[Client Tab] [Ajax Tab]
<click on 'Ajax Tab'>
[Client Tab] [Loading...]
<wait...>
[Client Tab] [Ajax Tab]
<click on 'Client Tab'>
[Client Tab] [Loading...]

研究

我在RichFaces' JIRA中发现了一个问题,但自3.2.1版以来,这个问题已得到解决。此外,我用谷歌搜索了几个小时,但未能找到任何类似的问题。

环境

  • Tomcat 7.0.30
  • Mojarra 2.1.12
  • RichFaces 4.2.2.Final

代码(缩减)

JSF

<ui:composition template="WEB-INF/templates/default.xhtml">
    <ui:define name="content">
        <h:form id="form">
            <rich:tabPanel id="tabPanel" switchType="client">
                <rich:tab id="clientTab">
                    <f:facet name="header">
                        <h:outputText value="Client Tab" />
                    </f:facet>
                    <h:outputText value="Foo" />
                </rich:tab>
                <rich:tab id="ajaxTab" switchType="ajax" status="ajaxTabStatus">
                    <f:facet name="header">
                        <a4j:status id="ajaxTabStatus">
                            <f:facet name="start">
                                <h:outputText value="Loading..." />
                            </f:facet>
                            <f:facet name="stop">
                                <h:outputText value="Ajax Tab" />
                            </f:facet>
                        </a4j:status>
                    </f:facet>
                    <h:outputText value="#{bean.value}" />
                </rich:tab>
            </rich:tabPanel>
        </h:form>
    </ui:define>
</ui:composition>

Ajax选项卡的Backing Bean

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private static final long serialVersionUID = -8405248908693334970L;

    private String value = "noValue";

    private static final Logger log = LoggerFactory.getLogger(Bean.class);

    @PostConstruct
    public void init() {
        log.info("Init bean.");
        value = getDummyValueWithFakeDelay();
    }

    public String getValue() {
        log.info("Get value.");
        return value;
    }

    private String getDummyValueWithFakeDelay() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
        }

        return DateFormat.getTimeInstance().format(new Date());
    }
}

更新1:

我发现<rich:someTag ... status="someStatus" />的标识符(我指的是a4j:status指定的标识符)不是id,而是name属性。所以我的a4j:status仅用于该页面上的每个 Ajax请求。给它命名并使用这个命名状态不起作用,因为现在什么也没发生。

<rich:tab id="ajaxTab" switchType="ajax" status="ajaxTabStatus">
    <f:facet name="header">
        <a4j:status id="ajaxTabStatus" name="ajaxTabStatus">
            <f:facet name="start">
                <h:outputText value="Loading..." />
            </f:facet>
            <f:facet name="stop">
                <h:outputText value="Ajax Tab" />
            </f:facet>
        </a4j:status>
    </f:facet>
    <h:outputText value="#{bean.value}" />
</rich:tab>

更新2:

我还发现了另一种方法,使用onbeginoncomplete属性和一些代码,如:

<rich:tab id="ajaxTab" switchType="ajax"
    onbegin="#{rich:component('ajaxTab')}.?"
    oncomplete="#{rich:component('ajaxTab')}.?">

好吧,如你所见,我还没有找到一种方法来操作标题标题(我没有找到任何有用的文档,说明我可以用rich:component做什么)。

2 个答案:

答案 0 :(得分:1)

我对RichFaces 4.x不是很熟悉。不过这是在RichFaces 3中如何做到这一点 假设为tabPanel

<h:form>
  <rich:tabPanel>
    <rich:tab label="Client Tab" switchType="client">
      Tab - client
    </rich:tab>

    <rich:tab label="Ajax Tab" id="ajaxTab" switchType="ajax" ontabenter="changeLabel(event)">
      <h:outputText value="#{bean.value}" />
    </rich:tab>
  </rich:tabPanel>
</h:form>

请注意ajax标签中的ontabenter事件。

现在你的脚本会是这样的。

<script>
    function changeLabel(tabId) {
        var tabLabel = event.target || event.srcElement;
        tabLabel.innerHTML = 'Loading...';
    }
</script>

我对RF4了解不多。但是,在RF3中,不需要向标签添加oncomplete事件。

答案 1 :(得分:1)

我使用richfaces 4.3.7执行下面的代码。它似乎工作。 (我显示加载面板而不是修改标签页。)

<a4j:status oncomplete="#{rich:component('busy')}.hide()"
    onerror="#{rich:component('busy')}.hide()"
    onbegin="#{rich:component('busy')}.show()">
    <rich:popupPanel id="busy" modal="true" moveable="false"
        resizeable="false" autosized="true">
        <h:panelGrid styleClass="alignCenter" width="100%" id="busyPanel"
            columns="1" border="0" cellpadding="0" cellspacing="2">
            <h:outputLabel value="Loading..." />
        </h:panelGrid>
    </rich:popupPanel>
</a4j:status>
<h:form>
    <rich:tabPanel switchType="ajax" 
        oncomplete="#{rich:component('busy')}.hide()" 
        onerror="#{rich:component('busy')}.hide()"
        onbegin="#{rich:component('busy')}.show()"
        >
        <rich:tab label="Client Tab" switchType="client">
          Tab - client
        </rich:tab>

        <rich:tab label="Ajax Tab" id="ajaxTab" >
          <h:outputText value="#{bean.value}" />
        </rich:tab>
    </rich:tabPanel>
</h:form>