How to use comments in JSF 2.0

Problem

In JSF 2.0, comment out a JSF tag like this

JSF…


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      >
     <h:body>

     <!-- 
    	<h:commandButton type="button" 
    		value="#{msg.buttonLabel}" />
      -->
    	  
    </h:body>
</html>

But JSF still process the value expression and output the result to the generated HTML page. Assuming that #{msg.buttonLabel} is return a “Submit” message.

Generated HTML page…


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
   <body> 
     <!-- 
    	<h:commandButton type="button" 
    		value="Submit" />
      -->
   </body> 
</html>

Is there a way to comment out a JSF tag completely? No process on the value expression or appear in the final generated HTML page?

Solution

There are two ways to comment out JSF tag :

1. facelets.SKIP_COMMENTS

In web.xml, set “facelets.SKIP_COMMENTS” parameter to “true“.


<context-param>
    <param-name>facelets.SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
</context-param>

Now, JSF removes anything in the page that is contained in <!– –>.

JSF…


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      >
     <h:body>

      <!-- 
    	<h:commandButton type="button" 
    		value="#{msg.buttonLabel}" />
       -->
    	  
     </h:body>
</html>

Generated HTML page…


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
	<body> 
    
	</body> 
</html>

2. ui:remove

Alternatively, you can use the “ui:remove” tag to define the content you want to remove. For example,

JSF


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
      <h:body>

        <ui:remove>
    	  <h:commandButton type="button" 
    		value="#{msg.buttonLabel}" />
        </ui:remove>
    	 
      </h:body>
</html>

Generated HTML page…


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
	<body> 
    
	</body> 
</html>

Download Source Code

Download It – JSF-2-Remove-Tag-Example.zip (10KB)

Reference

  1. JSF “ui:remove” JavaDoc

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

19 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Raymond Naseef
11 years ago

May want to update: facelets.SKIP_COMMENTS is deprecated … use javax.faces.FACELETS_SKIP_COMMENTS

Roland
6 years ago
Reply to  Raymond Naseef

The tutorial should be really updated then.

Maitis
7 years ago

Thank you, it works perfect

Maitis
7 years ago

Thank you

Lorenzo Lerate
9 years ago

I included the first solution in my web.xml configuration file. Now, my team and me can include comments in the .xhtml files. Thanks

Roland
6 years ago
Reply to  Lorenzo Lerate

Please note that javax.faces.FACELETS_SKIP_COMMENTS is now supposed to be used.

Andy
11 years ago

made my day!

Sam
12 years ago

Very good tutorial. Thank you!

aliali
13 years ago

thank you for this good tutorial

senthilkumar
13 years ago

Really doing to wonderful job. Keep it up

Leslie
14 years ago

Using Mojarra 2.1.7 now, “facelets.SKIP_COMMENTS” is deprecated. Use “javax.faces.FACELETS_SKIP_COMMENTS” instead.

Anonymous
15 years ago

A good catch. I was wondering what does JSF process the value expressions in the XML comments. In the Core Java Server Faces book I got to know that this feature was meant for use in JavaScript code inside comments.
Not having much knowledge about JavaScript, I was not able to clearly understand this. Any idea?

Anonymous
15 years ago
Reply to  Anonymous

In the above comment I meant “Why does JSF process the value expressions in the XML comments”.

Anonymous
15 years ago
Reply to  mkyong

As the book mentioned there is reason behind having this feature of processing the commented statements. It says that it is for use in the JavaScript code inside comments. I didn’t understood this, did you?

Ant
13 years ago
Reply to  Anonymous

In the old days, one used to put Javascript inside comments so that older browsers that didn’t support javascript would ignore it. Not sure its relevant today.

Belin
13 years ago

sorry but I do not see the interest of executed el inside comments. Why JSF unnecessarily complicates the work of programmers with behaviors that are obviously nonsense?

Roland
6 years ago
Reply to  Belin

is a HTML comment, not EL. That is why the EL code is still being executed. See javax.faces.FACELETS_SKIP_COMMENTS for a quick fix and you have your wished behavior.

arturj
11 years ago
Reply to  Belin

You are to young my child. Long time ago there was a decision about JavaScript in a comment block. This is a backward compatibility.