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
Note : You can find more similar articles at - JSF 2 Tutorials