tags:

views:

94

answers:

1

Hi,

I'm searching a supported way to render a section of code in JSF, I usually use this approach:

<ui:fragment rendered="#{condition}">
   <h:outputText value="text 1"/>
   <h:outputText value="text 2"/>
   <h:outputText value="text 3"/>
</ui:fragment>

Since ui:fragment doesn't support rendered most of IDE (like netbeans mark it as error BUT it works because in JSF parameters are inherited.

One way to solve this is to use another structure (for example if you use SEAM) you can use

<s:div rendered="#{condition}">
   ....
</s:div>

Another way is to set the rendered in all inner content like this:

<h:outputText value="text 1" rendered="#{condition}"/>
<h:outputText value="text 2" rendered="#{condition}"/>
<h:outputText value="text 3" rendered="#{condition}"/>

But I don't like this way because you have to add the rendered to each element.

Another way would be to use <c:if test="#{condtion}"> BUT c:if remove the elements from the JSF tree and that not what you always want to do especially if you use AJAX.

So you guys have another solutions?

+1  A: 

You can use a <h:panelGroup> for this.

<h:panelGroup rendered="#{condition}">
   <h:outputText value="text 1"/>
   <h:outputText value="text 2"/>
   <h:outputText value="text 3"/>
</h:panelGroup>

It outputs nothing if you don't specify the id, style, styleClass and like attributes, else a simple <span> will be rendered.

If all what you have is plain vanilla HTML (i.e. no JSF components), then you can also use <f:verbatim>.

<f:verbatim rendered="#{condition}">
   text 1
   text 2
   text 3
</f:verbatim>
BalusC