【问题标题】:JSF 2 Update Component Outside of Form and Outside of FaceletJSF 2 更新表单外和 Facelet 外的组件
【发布时间】:2012-08-27 04:33:02
【问题描述】:

我在 中有一个表单 当我使用 p:commandButton 保存数据时,我可以更新 中的表单和内容。但我无法弄清楚如何更新

中的组件

在顶部菜单中,我有一个面板的子视图,其中的链接根据一个人是否登录而呈现。当他们登录时,我会显示他们的用户名。如果我在模板的内容部分更新用户名,我想更新 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>

【问题讨论】:

  • render="@form :topMenuLoginForm" 并从该页面中删除 &lt;f:subview id="topMenuLoginView"&gt; 怎么样?
  • 稍后会尝试。我需要开始行动,刚刚选择在完成时刷新页面。我将在本周晚些时候进行重构。

标签: ajax templates jsf-2 primefaces facelets


【解决方案1】:

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

假设我们有一个元素&lt;p:commandButton&gt;

当元素没有给定 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。

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


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

【讨论】:

  • 我也尝试了更新路线。我很确定我也只尝试过 topmenuloginform 。我已经从不同的表单(用于实际登录某人的表单)访问 topmenuloginform,但该表单与 topmenuloginform 位于同一 topmenu 文件中。我会再试一次,因为也许出于某种原因我在脑海中尝试过它。不过,在本周晚些时候,不得不绕过它来完成一些事情。将在本周晚些时候重构。我只是在完成后刷新页面以使其现在可以正常工作。
【解决方案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

【讨论】:

  • 这是一种完全不同的情况,没有像问题中提到的那样的结构(例如,加载了组件的 ui:include 标签)。
猜你喜欢
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 2017-04-05
  • 1970-01-01
  • 2015-12-10
  • 2017-07-11
  • 2017-03-01
相关资源
最近更新 更多