tags:

views:

257

answers:

3

Hello, 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.

A: 

Not sure. An alternative though would be to use a template with a ui:insert and then direct to case1 or case2 which use ui:define programatically.

Drew
+1  A: 

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" />
McDowell
A: 

It is possible to have selective use of ui:include with other JSF components. Example:

<h:panelGroup rendered="#{!menuMBean.passwordResetRequired}">
    <ui:include src="homeNormal.xhtml" />
</h:panelGroup>
Martlark