Resolving facelets components at runtime

do you know a way to select a different facelets component at runtime?

I’ve got some of code similar to this:

<s:fragment rendered="#{r== 'case1'}">
     <div>
           <ui:include src="case1.xhtml" />
     </div>
 </s:fragment>
 <s:fragment rendered="#{r== 'case2'}">
     <div>
            <ui:include src="case2.xhtml" />
     </div>
 </s:fragment>

I’d like to write

<ui:include src="#{r}.xhtml" />

Thanks.

Answer

Your solution should work OK – the src attribute can be a literal or an EL expression. You might want to make the expression use a managed bean property or resolve it through a function. That way, you can ensure that it is never null (you could return a reference to an empty page if it was). You’ll probably get a 404 error if #{r} resolves to null.

<ui:include src="#{myfn:resolveNotNull(r, 'pageIfRIsNull')}.xhtml" />

Leave a Reply

Your email address will not be published. Required fields are marked *