How to dynamically change the content of a facet in a custom component?
Let's consider that I want to extend an existing JSF component, such as the <rich:datatable/>
. My main requirement is to dynamically modify the content of a <f:facet>
, to change its content.
What is the best way to achieve that? Or where is the best place in the code to achieve that?
In my faces-config.xml
, I have the following declaration:
<faces-config>
...
<component>
<component-type>my.component.dataTable</component-type>
<component-class>my.project.component.table.MyHtmlDataTable</component-class>
</component>
...
<开发者_JS百科render-kit>
<render-kit-id>HTML_BASIC</render-kit-id>
<renderer>
<component-family>org.richfaces.DataTable</component-family>
<renderer-type>my.renderkit.dataTable</renderer-type>
<renderer-class>my.project.component.table.MyDataTableRenderer</renderer-class>
</renderer>
...
Also, my my-project.taglib.xml
file (as I use Facelets) looks like:
<facelet-taglib>
<namespace>http://my.project/jsf</namespace>
<tag>
<tag-name>dataTable</tag-name>
<component>
<component-type>my.component.dataTable</component-type>
<renderer-type>my.renderkit.dataTable</renderer-type>
</component>
</tag>
So as you can see, I have two classes in my project for my custom datatable: MyHtmlDataTable
and MyDataTableRenderer
.
One of my idea is to modify the content of the <f:facet>
directly in the doEncodeBegin()
method of my renderer. This is working (in fact almost working), but I don't really think that's the better place to achieve my modification.
What do you think?
Technical information: JSF 1.2, Facelets, Richfaces 3.3.2, Java 1.6
I guess you can call getFacet(facetName)
and make the modifications on the returned component.
You can override getFacets()
(and/or getFacet(..)
), call the super
method and make the modifications on the returned value, and then return it.
精彩评论