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

Twitter

duminică, 7 iunie 2015

[OmniFaces utilities 2.0] Write component attributes of the given property-HTML mapping of names


[OmniFaces utilities] The writeAttributes() method writes component attributes of the given property-HTML mapping of names, if it's not empty. Map key will be used as component property name and map value will be used as HTML attribute name.

Method:
Usage:

A practical case of Renderers#writeAttributes() occurs when we write a custom component that extends an existing component and inherits its attributes. More precisely, we want to strictly refer to the mapping of component property-HTML attribute names of the attributes to be written (e.g. property-HTML mapping of names listed in HtmlGraphicImage, HtmlInputText, HtmlOutputText, etc). At rendering phase of our custom component we have two alternatives:

·         nominate each HTML attribute name and invoke the UIComponent#getAttributes()#get(component_property_name)
·         collect the mapping of component property-HTML attribute names of the attributes to be written in a Map and invoke Renderers#writeAttributes()

The first approach is a little bit clumsy. But, if we rely on the fact that JSF list the mapping of component property-HTML attribute names of the attributes to be written in a protected Java enum named, PropertyKeys, (see javax.faces.component.html classes) then we can write a snippet of code capable to collect this mapping for any JSF component, like this:

import org.omnifaces.util.Renderers;
...
public class MyComponent extends HtmlGraphicImage {
public class MyComponent extends HtmlOutputText {
public class MyComponent extends HtmlInputText {
...
public static final Map<String, String> ATTRIBUTE_NAMES = collectAttributeNames();

private static Map<String, String> collectAttributeNames() {
 Map<String, String> attributeNames = new HashMap<>();
 for (PropertyKeys propertyKey : PropertyKeys.values()) {
      String name = propertyKey.name();
      // 'class' is the HTML equivalent of 'styleClass'; since 'class' is a reserved
      // Java keyword it cannot appear in the enum.
      attributeNames.put(name, "styleClass".equals(name) ?
                               "class" : propertyKey.toString());
 }

 return Collections.unmodifiableMap(attributeNames);
}

Further, in a method specific to rendering (e.g. encodeBegin()), we can render the mapping, like below:

@Override
public void encodeBegin(FacesContext context) throws IOException {
 ResponseWriter writer = context.getResponseWriter();
 ...
 writeAttributes(writer, this, MyComponent.ATTRIBUTE_NAMES);
}

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors