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.Method:
Usage:
<?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:cc="http://xmlns.jcp.org/jsf/composite">
<!-- INTERFACE -->
<cc:interface>
<cc:attribute name="value"/>
<cc:attribute
name="to"/>
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
<p>#{cc.attrs.value},
#{cc.attrs.to}</p>
</cc:implementation>
</html>
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));
createValueExpression("#{welcomeBean.value}", java.lang.String.class));
composite.setValueExpression("to",
Components.
createValueExpression("#{welcomeBean.to}", java.lang.String.class));
createValueExpression("#{welcomeBean.to}", 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;
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}",
java.lang.String.class).getExpressionString());
java.lang.String.class).getExpressionString());
attributes.put("to", Components.createValueExpression("#{welcomeBean.to}",
java.lang.String.class).getExpressionString());
UINamingContainer
welcomeComponent = (UINamingContainer)
vdl.createComponent(context, "http://xmlns.jcp.org/jsf/composite/customs", "welcome", attributes);
vdl.createComponent(context, "http://xmlns.jcp.org/jsf/composite/customs", "welcome", attributes);
UIComponent
parent = context.getViewRoot().findComponent("welcomeId");
welcomeComponent.setId(parent.getClientId(context)
+ "_" + "welcomeMsgId");
parent.getChildren().add(welcomeComponent);
...
Complete
application for JSF 2.0 on GitHub
Complete
application for JSF 2.2 on GitHub
Niciun comentariu :
Trimiteți un comentariu