【问题标题】:partial page rendering (PPR) with faces-redirect带有面重定向的部分页面呈现 (PPR)
【发布时间】:2013-04-10 18:54:35
【问题描述】:

我使用的是 PrimeFaces 3.5,但我遇到了部分页面呈现的问题。我需要将 Facelet 文件中的内容加载到模板的“动态”部分。

index.xhtml:

<f:view> 
    <h:form id="form1">
        <p:outputPanel layout="block">
            <p:commandButton action="content-2?faces-redirect=false" update="display" />
        </p:outputPanel>
        <p:outputPanel id="display" layout="block">
            content 1
        </p:outputPanel>
    </h:form>
</f:view>

content-2.xhtml:

<h:body>
    content 2 loaded
</h:body>

当我点击&lt;p:commandButton&gt; 时,content-2.xhtml 将被打开。但是,这会刷新整个页面。 XML 响应包含如下内容:

<partial-response><changes><update id="javax.faces.ViewRoot">

当我将action 属性更改为方法表达式时:

<f:view> 
    <h:form id="form1">
        <p:outputPanel layout="block">
            <p:commandButton action="#{myBean.increment}" update="display" />
        </p:outputPanel>
        <p:outputPanel id="display" layout="block">
            #{myBean.count}
        </p:outputPanel>
    </h:form>
</f:view>

然后display 块按预期更新。 XML 响应包含如下内容:

<partial-response><changes><update id="form:display">

为什么action="content-2?faces-redirect=false"的方式会更新整个页面?

我也尝试&lt;ui:composition&gt;,但在这种情况下,这会重新加载模板的“静态”部分。我不想要。

【问题讨论】:

  • 您是在暗示#{myBean.method} 返回content-2.xhtmlfaces-redirect=false 已经是默认值了,我不明白你为什么要明确提到这一点。您是在暗示 action="content-2.xhtml" 的行为不同吗?
  • 不,抱歉,我确实用#{myBean.method} 更新了部分。方法体看起来像count++。我尝试action="content-2.xhtml" 它也刷新javax.faces.ViewRootaction="#{myBean.increment}" 刷新display。我认为faces-redirect=false 必须有分配truefalse

标签: ajax jsf-2


【解决方案1】:

如果您通过在action 属性中提供非null 结果导航 到不同的视图,这是完全正常和预期的行为。然后,JSF 将使用@all 的呈现来否决部分呈现。 JSF/Facelets 不会像您预期的那样以某种方式自动保留树中的公共组件。如果您打算在部分页面呈现期间动态更改组件树,那么您需要在 &lt;p:outputPanel id="display" layout="block"&gt; 中动态包含类似这样的内容(section 只是一个整数)

<ui:include src="section#{bean.section}.xhtml" />

或多个有条件渲染的部分

<ui:fragment rendered="#{bean.section == 1}"><ui:include src="section1.xhtml" /></ui:fragment>
<ui:fragment rendered="#{bean.section == 2}"><ui:include src="section2.xhtml" /></ui:fragment>
<ui:fragment rendered="#{bean.section == 3}"><ui:include src="section3.xhtml" /></ui:fragment>
<ui:fragment rendered="#{bean.section == 4}"><ui:include src="section4.xhtml" /></ui:fragment>

根据包含文件是否包含表单/输入/命令组件和/或引用视图范围的 bean,每个都有自己的优缺点。 &lt;ui:include&gt; 在视图构建期间运行,如 JSTL。另请参阅JSTL in JSF2 Facelets... makes sense?

faces-redirect=false 的存在并不重要,因为它已经是默认设置了。如果是true,则将在目标 URL 上调用 JavaScript window.location

【讨论】:

  • 谢谢,我想我现在明白了。我想我该如何做到这一点,页面上的所有块可能会独立于服务器状态进行更新。
猜你喜欢
  • 2019-03-09
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 2018-01-09
相关资源
最近更新 更多