Thursday, April 9, 2009

The facet thing

I have been looking for this in the Web my self, and could not find any real help.

Creating JSF components in dynamic way can bring you some troubles. Many JSF components, when using XHTML or JSP require Facets to render part of it's own body or content. Examples are Header of RichFaces panel or the like.

When creating dynamically a RichFaces panel for example, you will ran in to a problem if you want to create a header for your panel. To ilustrate this, let's look in to the way you create a RichFaces Panel when using the RichFaces tags:








As you may noticed, the problem one run on when creating this code in pure Java would be the Facet tag. If you look in to the JSF documentation, you will probably find the javax.faces.webapp.FacetTag which you might think of using it. But when creating a new instance of this class, you will certainly wonder how to use it, since this tag has nothing to do with JSF directly.
There is workaround for the following problem. Let us look in to the Java code on how to produce the same code from the above xml source:



HtmlPanel panel = (HtmlPanel)
FacesContext.getCurrentInstance().
getApplication().
createComponent(HtmlPanel.COMPONENT_TYPE);
HtmlOutputText text= (HtmlOutputText )
FacesContext.getCurrentInstance().
getApplication().
createComponent(HtmlOutputText .COMPONENT_TYPE);

text.setValue("My Header");

// now we will add the text to the panel's header

panel.getFacets().put("header",text);



As you see, that's all there it is to it. Every container component (correct me if I am wrong) in JSF has a Map of it's Facets. According to the JSF specification, the Facet (Tag) can contain only one UIComponent. So if you want more component's in your header, such as Text and Image at a time, you should wrap this components with one Html Panel Group instance and add it to your Facet Map.

No comments:

Post a Comment