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

I guess I'd call myself a problem solver. Either that, or I'm some sort of organic machine designed to convert oxygen into carbon dioxide. You could go either way on that. I'm into languages and stuff. I wrote a book. It's okay, I guess. What else? I like reading, swimming, eating, and playing music. Satisfied? Sheesh.

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, 5 years ago

Hello Roger,

Great blog.

Is there any email to contact you in private?

Regards,

Dimitris