Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional jsf include

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

like image 287
sasa Avatar asked Feb 18 '23 08:02

sasa


2 Answers

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;
}
like image 175
partlov Avatar answered Feb 19 '23 22:02

partlov


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
like image 25
Angel Salvador Ayala Ochoa Avatar answered Feb 19 '23 20:02

Angel Salvador Ayala Ochoa