7

我在 <ui:insert name="content" /> 中有一个表单当我使用 ap:commandButton 保存数据时,我可以更新 <ui:insert name="content" /> 中的表单和内容。但我无法弄清楚如何更新 <ui:include src="/sections/menus/topMenu.xhtml"/> 中的组件

在顶部菜单中,我有一个面板的子视图,其中的链接取决于一个人是否登录。当他们登录时,我会显示他们的用户名。如果我在模板的内容部分更新用户名,我想更新 ui 以便菜单中的名称也更新而无需刷新页面。但这是我也可以使用的后备方案。更新后刷新页面。但宁愿不要。如果您愿意,我会发布更多代码,但尽量将其保持在最低限度。

  <h:body>
    <div id="wrap">
      <div title="Container">
          <ui:include src="/sections/base/header.xhtml"/>
        </div><!-- End of Banner -->

        <div id="baseTemplateTopMenu" title="Menu">
          <ui:include src="/sections/menus/topMenu.xhtml"/>
        </div><!-- End of Menu -->

        <div id="sbt_contentbody"> 

            <div title="Left Column">
              <ui:include src="/sections/base/leftsidebar.xhtml"/>
            </div> <!-- End of Left Column -->
            <div title="Content Column">
              <ui:insert name="content" />
            </div><!-- End of Centre Column -->
            <div title="Right Column">
              <ui:include src="/sections/base/rightsidebar.xhtml"/>
            </div><!-- End of Right Column -->

        </div>
        <div id="footer" title="Footer" class ="container">
          <ui:include src="/sections/base/footer.xhtml"/>
        </div><!-- End of Footer -->

      </div><!-- End of Container -->
  </h:body>

下面是保存用户信息的 p:commandButton

<p:commandButton id="id_SubmitUserInfoPrefs"
                    value="Update"
                    action="#{userPreferences.updateUserInfo()}" styleClass="bottom_margin_2em top_margin_2em">
    <f:ajax execute="@form" render="@form :topMenuLoginView:topMenuLoginForm" />
<!-- tried adding 'update=":"topMenuLoginView:topMenuLoginForm"' as well. -->

下面是模板的菜单部分。

<f:subview id="topMenuLoginView">
    <h:form id="topMenuLoginForm" prependId="false">
        <ul>
            <h:panelGroup id="loginPanel" rendered="#{!user.loggedIn}">
                <li><h:outputLink value="javascript:void(0)" onclick="topMenuLoginDialog.show()" 
                                    title="login" styleClass="loginpanelclass">Log In</h:outputLink></li>
                <li><h:link value="Register" outcome="/registration/register.xhtml" /></li>
            </h:panelGroup>
            <h:panelGroup id="logoutPanel" rendered="#{user.loggedIn}">
                <li><h:link value="#{user.nickname}" outcome="#{navigationprops.userprefs}" /> </li>
                <li><p:commandLink action="#{webAuthenticationBean.logout}" 
                                    update=":topMenuLoginView:topMenuLoginForm">
                        Logout
                    </p:commandLink>
                </li>
            </h:panelGroup>
        </ul>
    </h:form>
</f:subview>
4

2 回答 2

9

通过 ID 引用元素的一般思路如下:

假设我们有一个 element <p:commandButton>

当元素没有 id 时,JSF 生成一个:

<button name="j_idt191" id="j_idt191" ....

如果一个元素嵌套在像表单这样的容器元素中,JSF 会在 ID 前面加上表单的 ID:

JSF:

<h:form>
    <p:commandButton/>
</h:form>

HTML:

<form id="j_idt48">
    <button name="j_idt48:j_idt191" id="j_idt48:j_idt191" ....
</form>

当另一个容器中有元素时,您需要从根元素引用。这可以使用ID 前面的“ : ”来完成。这是一个例子:

<p:commandButton id="button1" update=":form:button2" ...

<h:form id="form">
    <p:commandButton id="button2" update=":button1" ...
</h:form>

如果您不确定 JSF 为您的元素分配了什么 ID,请使用 FireBug 之类的工具来检查元素并发现它的 ID。

就您的代码而言,我认为不会<f:subview>生成容器元素,因此您需要topMenuLoginForm使用update=":topMenuLoginForm".


话虽如此,如果您想使用 AJAX 更新元素,请使用 update 属性。请参阅我上面的示例。

于 2012-08-27T21:48:25.953 回答
2

更新窗体外的组件

<p:gmap center="36.890257,30.707417" zoom="13" type="ROADMAP" 
                style="width:600px;height:400px"
                model="#{mapBean.simpleModel}"
                onPointClick="handlePointClick(event);"
                widgetVar="cliMap" fitBounds="true"
                id="map">
            <p:ajax event="overlaySelect" listener="#{mapBean.onMarkerSelect}"  />
        </p:gmap>

        <p:dialog widgetVar="dlg" showEffect="fade">
            <h:form prependId="false" id="formnew">
                <h:panelGrid columns="2">
                    <f:facet name="header">
                        <p:outputLabel value="New Point"/>
                    </f:facet>
                    <h:outputLabel for="title" value="Title:" />
                    <p:inputText id="title" value="#{mapBean.title}" />

                    <f:facet name="footer">
                        <p:commandButton value="Add" actionListener="#{mapBean.newPoint()}" update=":map" oncomplete="markerAddComplete()" />
                        <p:commandButton value="Cancel" onclick="return cancel()" />
                    </f:facet>
                </h:panelGrid>
                <h:inputHidden id="Nlat" value="#{mapBean.lat}"  />
                <h:inputHidden id="Nlng" value="#{mapBean.lng}" />

            </h:form>
        </p:dialog>

我使用 update=":map" 以使用 Primefaces 的形式更新谷歌地图

于 2014-05-22T08:26:57.850 回答