My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Se încarcă...

Petition by Java EE Guardians

Twitter

vineri, 13 februarie 2015

[OmniFaces utilities (2.0)] Include the Facelet file at the given (relative) path as child of the given UI component parent


[OmniFaces utilities] The includeFacelet() include the Facelet file at the given (relative) path as child of the given UI component parent. This has the same effect as using <ui:include>. The path is relative to the current view ID and absolute to the web content root.

Method:
Usage:

JSF main page - when the button is clicked, we will include in it some Facelets:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
 <h:head>
  <title></title>
 </h:head>
 <h:body>
  <h:form>
   <h:commandButton value="Include facelet programmatically" action="#{faceletsBean.addFaceletAction()}"/>
  </h:form>
 </h:body>
</html>

JSF pages to be included. The parent component will be UIViewRoot:

·         fileA.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

 <ui:composition>           
  <h:outputText value="File A|" style="color: #cc0000;"/>
 </ui:composition>
</html>

·         fileB.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

 <ui:composition>           
  <h:outputText value="#{bparam}" style="color: #00cc00;"/>
 </ui:composition>
</html>

·         fileC.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

 <ui:composition>           
  <h:outputText value="#{cparam}" style="color: #0000cc;"/>
 </ui:composition>
</html>

Now, the programmatically approach is implemented in addFaceletAction() method, as below:

·         using OmniFaces approach (shortcut)

import org.omnifaces.util.Components;
import org.omnifaces.util.Faces;
...
Components.includeFacelet(Faces.getViewRoot(), "/files/fileA.xhtml");
Faces.getFaceletContext().setAttribute("bparam", "file B - text as ui:param via string literal...");
Components.includeFacelet(Faces.getViewRoot(), "/files/fileB.xhtml");
Faces.getFaceletContext().setAttribute("cparam", cfiletext);
Components.includeFacelet(Faces.getViewRoot(), "/files/fileC.xhtml");

·         using classical approach

FacesContext context = FacesContext.getCurrentInstance();
FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);

faceletContext.includeFacelet(context.getViewRoot(), "/files/fileA.xhtml");
faceletContext.setAttribute("bparam", "file B - text as ui:param via string literal...");
faceletContext.includeFacelet(context.getViewRoot(), "/files/fileB.xhtml");
faceletContext.setAttribute("cparam", cfiletext);
faceletContext.includeFacelet(context.getViewRoot(), "/files/fileC.xhtml");

In both cases the output will be like in figure below (left side - before inclusion, right side -  after inclusion):

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors