Conditionally printing HTML tags in JSF Facelets

This took me a bit to figure out so I thought I’d post it.

Say you’re using JSF and want to conditionally print some HTML tags in your Facelet that could, technically speaking, result in non well-formed markup. You can’t really do it. Printing something like is illegal as it’ll complain about the < and > characters being in there. Using the tag doesn’t work either, even if you wrap the content in a CDATA element. Problem is that it’s preventing you from writing non well-formed markup in any which way.

One of the solutions is to force the rendering kit to not analyze the output for “well-formedness” by passing the HTML markup in a variable.

In your facelet.xhtml

<h:outputText escape="false" 
              rendered="#{someCondition}" 
              value="#{messages&#91;'lessThan'&#93;.concat('ul').concat(messages&#91;'greaterThan'&#93;)}"/>

In messages.properties

lessThan=<
greaterThan=>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s