Partial page update when navigating (PrimeFaces ajax)

有些话、适合烂在心里 提交于 2019-11-29 23:54:40

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/5685354/partial-page-update-when-navigating-primefaces-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!