Usage of the facelets tag ui:remove

Goal

Understand the usage of the facelets tag ui:remove

Description

I have been working with JSF and Facelets in the last couple of years but, to tell you the truth, it has been a while since I read a (good) book on the subject.

Recently, I managed to get some time to read about that subject in more detail and one of the things I learned from the book Core Java Server Faces – 3rd edition is about the usage of the tag ui:remove.

This tag is used to remove from the JSF component tree any of the tags within it. Bearing that in mind, this recipe provides a practical example on when this tag can be handy: when we want to find out which part of a JSF page is causing a problem and we need to “comment” some parts of that page. Do you know what the specification of the parameter javax.faces.FACELETS_SKIP_COMMENTS as true does? If you do, then what if we want the comments not to be excluded from the generated pages, by letting that property undefined or set to false, and we comment some parts of our page to try to understand if that is the one causing the problem? Well, actually, an XML comment block, such as <!– –>, will do no good because JSF will still process the expression languages inside it!

How to

First, lets assume we have a page that, when being rendered, makes the error page to be shown. When looking at the logs, we see that an exception is thrown but, from the stacktrace, we cannot infer what is causing it.

So, instead of commenting some parts of our markup, we will wrap the markup we want excluded with the facelets tag ui:remove. Something similar to:

...
<ui:remove>
  <h:commandButton id="loginButton" value="#{msgs.loginButtonText}" action="home" />
</ui:remove>
...

Explanations

By using the tag ui:remove instead of an XML comment, we get the chance of actually removing parts of the page as if they were being commented! So, funny but, “removing” through facelets is like commenting but better than commenting because the comments are still processed!

According to the book Core Java Server Faces – 3rd edition, this is the reason why facelets process value expressions in XML comments: “this feature was meant for use in JavaScript code inside comments.”

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