Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

java - Alternative to ui:fragment in JSF

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

This is actually a bug in JSF 2.0 Facelets tag file declaration (in Mojarra, that's the com/sun/faces/metadata/taglib/ui.taglib.xml file). The rendered attribute is overlooked and missing in the tag file declaration (and also in the JSF 2.0 <ui:fragment> tag documentation), while it is really present in the UIComponent. The IDE validation is based on the tag file declarations and hence it gives a misleading validation error. This issue is fixed in JSF 2.1, the missing attribute is added to the tag file declaration (and also in the JSF 2.1 <ui:fragment> tag documentation).

If either just ignoring the IDE warnings or upgrading to JSF 2.1 is not an option, then you can consider using the <h:panelGroup> component instead:

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

It outputs nothing anyway 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 consider using <f:verbatim>.

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

However, the <f:verbatim> is deprecated in JSF 2.0 (which in turn has also a documentary bug by the way, the JSF 2.0 <f:verbatim> tag documentation does not mention deprecation, but the JSF 2.1 <f:verbatim> tag documentation does).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...