JSF Composite Components Where You Want Them

By , 6 July 2012

JSF Composite Components Where You Want Them
JSF Composite Components Where You Want Them

As Ryan Lubke commented in the Facelets Tags vs Composite Components thread, JSF composite components must be kept in the /resources directory in your webapp. This grates against my "package-by-feature" style of programming. I want all my related code together thanks.

Fortunately there are two ways to work around this problem.

  1. Symlinks. Standard Unix symlinks from the resources directory work fine for me. Symlinks tend to be a bit flaky though, so you might prefer to try...
  2. Decoration. Facelets knows no bounds when it comes to organising your templates. You can use normal Facelets decoration to redirect your xhtml templates under the resources directory to the real implementation:
    <ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                    template="/apps/webcore/tags/selectUser.xhtml"/>
JSF Composite Components Where You Want Them

That's not perfect, but it lets me keep all my related code together. To make things a little neater, you can actually use the same namespace for composite components and regular components. Just declare the location of your composite resources directory in the taglib.xml like this:

 <facelet-taglib>
   <namespace>https://rogerkeays.com/webcore</namespace> 
   <composite-library-name>webcore</composite-library-name> 
 </facelet-taglib>

I don't see any reason why composite components should not be able to be added to taglib.xml. If I had it my way, the existing Facelets <tag> feature would just look in the tag file for <cc:implementation> and handle it as a composite component if one is found.

In fact, composite components and tag files should be the same thing.

About Roger Keays

JSF Composite Components Where You Want Them

Roger Keays is an artist, an engineer, and a student of life. He has no fixed address and has left footprints on 40-something different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, the proper use of semicolons, and finding good food.

Leave a Comment

Please visit https://rogerkeays.com/jsf-2-0-composite-components-where-you-want-them to add your comments.

Comment posted by: Dimitris Kalogirou, 11 years ago

Hello Roger,

Great blog.

Is there any email to contact you in private?

Regards,

Dimitris