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

miercuri, 13 ianuarie 2016

JSF 2.2 [usage pitfall] - Prevent unintuitive behavior of nested tag files

Consider the following custom tag structure:

<my:tag id="foo" name="fooName">
 The id, <strong>#{id}</strong>, belongs to, <strong>#{name}</strong>
 <my:tag>
  The id, <strong>#{id}</strong>, belongs to, <strong>#{name}</strong>
 </my:tag>
</my:tag>

Check out the nested <my:tag>! Even if we don't specify the values of the id and name attributes, this tag has inherited them from the attributes of the parent tag with the same name. So, you will see:

The id, foo, belongs to, fooName
The id, foo, belongs to, fooName

While you may expected to see:

The id, foo, belongs to, fooName
The id,, belongs to,

Moreover, the problem doesn't manifest only when you nest very same tag file. But the problem applies as good on nesting completely different tags. For example:

<my:foo>
 <my:bar>
  <my:baz>

The issue of nested custom tags consist in the fact that the unspecified attributes will inherit the values from the attributes of their parents with the same name, instead of defaulting to null (empty string).

When this behavior represents an issue ( a big step consist in identifying the issue) you have to find an workaround to avoid it. Of course, the simplest approach consist in explicitly set the attributes values, but this is not always needed or convenient.

A professional workaround consist in using the OmniFaces TagAttribute. This allows us to explicitly declare a tag attribute on a Facelets tag file and avoid the presented issue. The attribute indicated via this tag handler will not inherit its value from the homologous attribute of the parent tag with the same name. Instead of this default behavior, the TagAttribute will clear out the attribute value and will support a default value for it. For page authors, the TagAttribute is available via the <o:tagAttribute> tag and configurable via two attributes:

·         name - This attribute is required and it represents the declared attribute name.
·         default - This attribute is optional and it represents the default value of the declared attribute.

This takes effect only when the declared attribute actual value is null.

<html xmlns=...>
 <ui:composition>
  <o:tagAttribute name="id" />
  <o:tagAttribute name="name" default="buzzName" />
  <div>
   <ui:insert />
  </div>
 </ui:composition>
</html>
Now, when the id and name are not present in the nested tag, we will see:

The id, foo, belongs to, fooName
The id,, belongs to, buzzName

If we don't indicate a default value for the name attribute then we will see:

The id, foo, belongs to, fooName

The id,, belongs to,

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors