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

19 comments on “How to use comments in JSF 2.0

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

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

      Reply
  2. 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?

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

      Reply
    2. 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.

      Reply
  3. Really doing to wonderful job. Keep it up

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

    Reply
  5. 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?

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

      Reply
        1. 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?

          Reply
          1. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *