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

Petition by Java EE Guardians


duminică, 22 februarie 2015

[OmniFaces utilities (2.0)] Programmatically create and include a composite component (JSF 2.0/2.2)

This method was improved starting with OmniFaces 2.2 with the possibility to set attributes on the composite component.

[OmniFaces utilities] The includeCompositeComponent() method creates and include the composite component of the given library and resource name as child of the given UI component parent and return the created composite component. This has the same effect as using <my:resourceName>. The given component ID must be unique relative to the current naming container parent and is mandatory for functioning of input components inside the composite, if any.


In this post you will see how to programmatically create and include in page a composite component. We will cover JSF 2.0 and JSF 2.2. First, we need a composite component, and we will use a simple, pretty dummy one:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""

 <!-- INTERFACE -->
  <cc:attribute name="value"/>
  <cc:attribute name="to"/>  

  <p>#{cc.attrs.value}, #{}</p>

In order, to programmatically include this composite component, you have to now several things:
·         the parent of this composite component (e.g. <h:panelGroup>)
·         the library name of this composite component (e.g. customs)
·         the composite component resource name (e.g. /welcome.xhtml)
·         the composite component ID (e.g. welcomeMsgId)
Based on these information, you can create and include the composite component via OmniFaces utilities, Components.includeCompositeComponent() method, as below:

import org.omnifaces.util.Components;
import org.omnifaces.util.Faces;
UIComponent parent = Faces.getViewRoot().findComponent("welcomeId");
UIComponent composite = Components.includeCompositeComponent(parent, "customs", "/welcome.xhtml", "welcomeMsgId");

Further, we can set the attributes (value and to) values using the OmniFaces utilities, Components.createValueExpression():

composite.setValueExpression("value", Components.
   createValueExpression("#{welcomeBean.value}", java.lang.String.class));       
composite.setValueExpression("to", Components.
   createValueExpression("#{}", java.lang.String.class));       

Starting with JSF 2.2, we can use an explicit API for instantiating composite components programmatically. The core of this API is based on the new createComponent() method added in the ViewDeclarationLanguage class. The signature of this method is as follows:

public UIComponent createComponent(FacesContext context, String
taglibURI, String tagName, Map<String,Object> attributes)

Besides FacesContext, you need to pass the tag library URI, the tag name, and the tag's attributes, or null, if there are no attributes. For example, the Welcome component can be added via this API as follows (we append the Welcome component to a <h:panelGroup> with the welcomeId ID):

import org.omnifaces.util.Components;
FacesContext context = FacesContext.getCurrentInstance();
ViewDeclarationLanguage vdl = context.getApplication().getViewHandler().getViewDeclarationLanguage(context, context.getViewRoot().getViewId());

Map<String, Object> attributes = new HashMap<>();
attributes.put("value", Components.createValueExpression("#{welcomeBean.value}",
attributes.put("to", Components.createValueExpression("#{}", java.lang.String.class).getExpressionString());
UINamingContainer welcomeComponent = (UINamingContainer) 
   vdl.createComponent(context, "", "welcome", attributes);
UIComponent parent = context.getViewRoot().findComponent("welcomeId");
welcomeComponent.setId(parent.getClientId(context) + "_" + "welcomeMsgId");

Complete application for JSF 2.0 on GitHub
Complete application for JSF 2.2 on GitHub

Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors