2011-04-16 43 views
1

我已经使用facelets模板完成了一个基本的JSF应用程序。我的模板如下:导航时部分页面更新(PrimeFaces ajax)

<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:p="http://primefaces.prime.com.tr/ui" 
     xmlns:ui="http://java.sun.com/jsf/facelets"> 

    <h:head> 
     <!-- Links to CSS stylesheets... --> 
     <title><ui:insert name="title" /> - FManager</title> 
    </h:head> 

    <h:body> 
     <div id="wrapper"> 
      <div id="header"> <b>FManager</b> Application </div> 

      <div id="content"> 
       <p:growl id="growl" /> 
       <ui:insert name="content"> 
        Sample content. 
       </ui:insert> 
      </div> 
     </div> 

     <div id="footer">Made using JSF and Primefaces</div> 
    </h:body> 
</html> 

然后,我有一个主页(如下图所示),导航到一个第二页。这两个页面都使用上面的模板。

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets" 
       xmlns:h="http://java.sun.com/jsf/html" 
       xmlns:p="http://primefaces.prime.com.tr/ui" 
       template="./template.xhtml"> 

    <ui:define name="title">Main page</ui:define> 

    <ui:define name="content"> 
     <h2>Welcome to the <b>FManager</b> application</h2> 

     <h:form> 
      <p>Click the button below to create the first entry!</p> 
      <p:commandButton value="Create entry" action="create" /> 
     </h:form> 
    </ui:define> 

</ui:composition> 

如果我faces-config.xml中,将定位用<redirect/>,但页面重新加载。我的问题是:

有没有办法从一个页面导航到另一个只更新模板的<ui:insert>部分?(同时保持页面的其余部分不变)

谢谢!

回答

4

您可以通过ajax重新加载内容,但URL将保持不变。如果你能负担得起的,按以下步骤更新代码:

template.xhtml,通过

<h:panelGroup id="content" layout="block"> 

而且在somepage.xhtml取代

<div id="content"> 

,更换

<p:commandButton value="Create entry" action="create" /> 

<p:commandButton value="Create entry" action="create" update=":content" /> 

终于摆脱了<redirect />(实际上,喜欢的所有导航案件get rid因为它们使faces-config.xml文件臃肿)的。顺便也可以在结果中指定重定向,如action="create?faces-redirect=true"

这不会更新<title>。但是,您可以使用简单的JavaScript行来完成此操作。