4

How can I conditionally include a jsf facelets file at runtime? sample functionality required is

if ( add button click) {

ui:include src="Add.xhtml"
}

if ( update button click) {

ui:include src="Update.xhtml"
}

the syntax above is only indicative ...

Mojarra 2.1.1 / Apache Tomcat 7.0.22 / PrimeFaces 3.4

HemChe
  • 2,249
  • 1
  • 21
  • 33
sasa
  • 197
  • 1
  • 3
  • 13

2 Answers2

7

ui:include doesn't have rendered attribute, so you have to encapsulate it in some other component. Also, you will set some property on server base on button clicked.

<h:form>
  <p:commandButton value="Add" update=":includeContainer">
    <f:setPropertyActionListener value="add" target="#{myBean.action}"/>
  </p:commandButton>
  <p:commandButton value="Update" update=":includeContainer">
    <f:setPropertyActionListener value="update" target="#{myBean.action}"/>
  </p:commandButton>
</h:form>

<h:panelGroup id="includeContainer">
  <h:panelGroup rendered="#{myBean.action == 'add'}">
    <ui:include src="add.xhtml"/>
  </h:panelGroup>
  <h:panelGroup rendered="#{myBean.action == 'update'}">
    <ui:include src="update.xhtml"/>
  </h:panelGroup>
</h:panelGroup>

in backing bean you will have getter and setter:

public void setAction(String action) {
  this.action = action;
}

public String getAction() {
  return action;
}
wittich
  • 2,079
  • 2
  • 27
  • 50
partlov
  • 13,789
  • 6
  • 63
  • 82
  • a substitute question to this is that if its a conditional include then why is the xhtml processed when the main file is called ... i checked this by including a f:event prerenderview tag in the add.xhtml file for example ... i get the system.out printed via the listener method ... yes the view scoped bean is not instantiated but the xhtml is parsed and prerenderview event triggered ... why is it so when its a conditional include? – sasa Mar 04 '13 at 17:15
  • 1
    This is because `ui:include` is executed during build view phase, and rendered attribute is calculated later during render view phase. That is the way this works in JSF. If this is a problem you must consider another solutions. Consider [this answer](http://stackoverflow.com/questions/11989631/skip-executing-uiinclude-when-parent-ui-component-is-not-rendered/11991895#11991895) for solutions. – partlov Mar 04 '13 at 18:40
  • @ᴠɪɴᴄᴇɴᴛ Fixed. Thanks. – partlov Jan 12 '16 at 14:46
1

i repost the answer of partlov becaus have some error and i corect this errors an put in this pos, and congratulatiosn partlov to much good answer

i complement your answer first this is a xhtml page whith prime faces, if you want use a p: add this frameworc or an other.

if you wont download this framewor doenload hereDownload prime Framework for JSF

and impor for this way

xmlns:p="http://primefaces.org/ui"

select.XHTML

<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
  <h:form>
    <p:commandButton value="Add" update="panel">
      <f:setPropertyActionListener value="add" target="#{myBean.action}"/>
    </p:commandButton>
    <p:commandButton value="Update" update="panel">
      <f:setPropertyActionListener value="update" target="#{myBean.action}"/>
    </p:commandButton>


    <h:panelGroup id="panel">
      <h:panelGroup rendered="#{myBean.action == 'add'}">
        <ui:include src="headerLogin.xhtml"/>
      </h:panelGroup>
      <h:panelGroup rendered="#{myBean.action == 'update'}">
        <ui:include src="Pajax.xhtml"/>
      </h:panelGroup>
    </h:panelGroup>
  </h:form>
</h:body>

nex step is a create a clas myBean and use this string for a select whath UI you want render myBean.java

use this imports in a bean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

and use this cod in a clas

public class myBean {
String action;
public void setAction(String action) {

    this.action = action;
}

public String getAction() {
  return action;
}

}

but remember put this line up to a class

@ManagedBean
@SessionScoped
  • Is this example working? I copy pasted the code and tried but it didn't work as expected. I need to refresh the page to go on next option. Any help is appreciated. – ran_pal Sep 08 '14 at 04:10