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