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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
12 Comment threads
5 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
13 Comment authors
MaitisLorenzo LerateAndyarturjRaymond Naseef Recent comment authors
newest oldest most voted
Raymond Naseef
Guest
Raymond Naseef

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

Maitis
Guest
Maitis

Thank you, it works perfect

Maitis
Guest
Maitis

Thank you

Lorenzo Lerate
Guest
Lorenzo Lerate

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

Andy
Guest
Andy

made my day!

Sam
Guest
Sam

Very good tutorial. Thank you!

Belin
Guest
Belin

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?

arturj
Guest
arturj

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

aliali
Guest
aliali

thank you for this good tutorial

trackback
JSF 2.0 Tutorial

[…] JSF 2 remove example JSF 2 <ui:remove> example. […]

senthilkumar
Guest
senthilkumar

Really doing to wonderful job. Keep it up

Leslie
Guest
Leslie

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

Anonymous
Guest
Anonymous

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
Guest
Anonymous

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