0

I would like to update a ui:include dynamically. I tried to implement it with the following codes :

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

<h:head>
   <title>Title</title>
</h:head>

<h:body>

<p:layout fullPage="true" resizeTitle="resize"
    style="background-color:#FFFFFF;">

    <p:layoutUnit position="north" id="north" size="130" resizable="false"    closable="false">
        <ui:include src="header.xhtml" />
    </p:layoutUnit>

    <p:layoutUnit position="west" id="west" size="231" resizable="false" closable="false" style="overflow:hidden;" >
        <h:form>
            <ui:include src="menu.xhtml" />
        </h:form>
    </p:layoutUnit>

    <p:layoutUnit styleClass="layoutUnitCenter" position="center">
        <h:form id="mainForm">
            <ui:insert name="content" />
        </h:form>
    </p:layoutUnit>
</p:layout>
</h:body>

</html>

The client :

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
template="WEB-INF/templates/layout.xhtml" >


   <ui:define name="content">
       <ui:include src="pages/#{appView.content}.xhtml" />
   </ui:define>
</ui:composition>

With AppView.java :

@ManagedBean
@SessionScoped
public class AppView {

  private String content;

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  @PostConstruct
  public void init() {
      this.content = "tiers/search"; 
  }

}

The first page is a search page. When I do a search and find something, I try to update "content" to include the page with the result instead.

search.xhtml

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

<h:form id="form" title="Recherche">

    <p:growl id="msgs" showDetail="true" />

    <p:panelGrid columns="2" cellpadding="5">
        <p:outputLabel for="numPersonne" value="Numéro Personne" />
        <p:inputText id="numPersonne" size="20" value="#{ficheRechercheView.numPersonne}" />

        <p:outputLabel for="numSiren" value="Numéro SIREN" />
        <p:inputText id="numSiren" size="20" value="#{ficheRechercheView.numSiren}" />

        <p:outputLabel for="numInternatl" value="Numéro International" />
        <p:inputText id="numInternatl" size="50" value="#{ficheRechercheView.numInternatl}" />

        <p:outputLabel for="raisonSociale" value="Raison sociale" />
        <p:inputText id="raisonSociale" size="50" value="#{ficheRechercheView.raisonSociale}" />
    </p:panelGrid>

    <p:commandButton value="Valider" action="#{searchView.search}" icon="ui-icon-check" />
</h:form>
</ui:composition>

And SearchView.java has AppView.java as a managed property.

@ManagedProperty(value="#{appView}")
private AppView appView; 

public void search() {
    ....

    appView.setContent("tiers/detail");
    RequestContext context = RequestContext.getCurrentInstance();
    context.update("content");
}

I only get the updated display if I update the page.

As an aside question, should I manage navigation this way or just load different pages using the same template everytime ?

Sandy
  • 547
  • 3
  • 9
  • 20
  • 1
    Does this help? http://stackoverflow.com/q/7108668 – BalusC Jan 26 '16 at 18:05
  • Like a charm, thanks a lot. Saw that topic floating around when I was dealing with other problems but couldn't find it again after. I upvoted your comment but if you make an answer with the link I'll select it as the solution. – Sandy Jan 27 '16 at 07:28

0 Answers0