导航时部分页面更新(PrimeFaces ajax) [英] Partial page update when navigating (PrimeFaces ajax)
问题描述
我使用 facelets 模板完成了一个基本的 JSF 应用程序.我的模板如下:
<h:头><!-- CSS 样式表的链接... --><title><ui:insert name="title"/>- FManager</h:head><h:body><div id="包装器"><div id="标题"><b>FManager</b>应用程序
<div id="内容"><p:grow id="growl"/><ui:insert name="content">示例内容.</ui:insert>
<div id="footer">使用 JSF 和 Primefaces 制作</div></h:body>
然后我有一个主页(如下所示)导航到第二页.两个页面都使用上面的模板.
<ui:define name="title">主页</ui:define><ui:define name="content"><h2>欢迎来到<b>FManager</b>应用<h:形式><p>点击下面的按钮创建第一个条目!</p><p:commandButton value="Create entry" action="create"/></h:form></ui:define></ui:composition>
如果我在 faces-config.xml 中使用
,它会导航,但会重新加载 完整 页面.我的问题是:
有没有办法从一个页面导航到另一个页面,只更新模板的 <ui:insert>
部分?(同时离开页面的其余部分完好无损)
谢谢!
您可以通过 ajax 重新加载内容,但 URL 将保持不变.如果您负担得起,请按如下方式更新代码:
在template.xhtml
中,替换
由
<小时>
在somepage.xhtml
中,替换
<p:commandButton value="Create entry" action="create"/>
由
<p:commandButton value="Create entry" action="create" update=":content"/>
<小时>
终于去掉<redirect/>
(其实更喜欢摆脱 所有导航案例,因为它们使 faces-config.xml
文件膨胀).顺便说一下,也可以在结果中指定重定向,如 action="create?faces-redirect=true"
.
这只是不更新
.但是,您可以使用简单的 JavaScript 代码行来做到这一点.
I have done a basic JSF app, using facelets templates. My template is as follows:
<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>
And then I have a Main Page (shown below) that navigates to a Second Page. Both pages use the template above.
<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>
If I use <redirect/>
in faces-config.xml, it navigates, but the full page is reloaded. My question is:
Is there a way to navigate from a page to another updating ONLY the <ui:insert>
sections of the template? (While leaving the rest of the page intact)
Thanks!
解决方案 You can reload content by ajax, but URL will stay the same. If you can afford that, update the code as follows:
In template.xhtml
, replace
<div id="content">
by
<h:panelGroup id="content" layout="block">
And in somepage.xhtml
, replace
<p:commandButton value="Create entry" action="create" />
by
<p:commandButton value="Create entry" action="create" update=":content" />
Finally get rid of the <redirect />
(actually, prefer to get rid of all navigation cases since they make the faces-config.xml
file bloated). A redirect can by the way also be specified in the outcome as in action="create?faces-redirect=true"
.
This does only not update the <title>
. You could however do that with a simple JavaScript line.
这篇关于导航时部分页面更新(PrimeFaces ajax)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文