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>








Using Mojarra 2.1.7 now, “facelets.SKIP_COMMENTS” is deprecated. Use “javax.faces.FACELETS_SKIP_COMMENTS” instead.
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?
In the above comment I meant “Why does JSF process the value expressions in the XML comments”.
This is how JSF work :), and you can skip it easily, see above solution.
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?