Sometimes, to find out which part of a jsf view causes a stack trace, you may want to use the time-honored divide-and-conquer strategy of commenting out parts of the page to isolate the offending component.
Somewhat surprisingly, the XML comments <!– … –> are not useful for this purpose for example when you comment out a button in an XHTML page, JSF will process the value expression, and place the result, as a comment, in the generated HTML page. In case the expression throws an exception, then the XML comments don’t help you at all. Since JSF 2.0 a new Tag has provided for commenting the undesired components. Instead of using comments JSF provides you a new Tag ui:remove.
Also Read:
1.Managed Bean
IndexBean.java
package net.javabeat.jsf.data; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean @SessionScoped public class IndexBean { private String evaluatedProperty = "Please Use ui:remove rather using of XML comments"; private String evaluatedMessage = "Am I Evaluated"; public String getEvaluatedProperty() { System.out.println("Even you're commenting out my associated output component, " + "however it executes the getEvaluatedProperty." + "Using ui:remove will cause this expression to be not executed"); return evaluatedProperty; } public void setEvaluatedProperty(String evaluatedProperty) { this.evaluatedProperty = evaluatedProperty; } public String getEvaluatedMessage() { System.out.println("Evaluated"); return evaluatedMessage; } public void setEvaluatedMessage(String evaluatedMessage) { this.evaluatedMessage = evaluatedMessage; } }
2. The View
index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <h:outputScript library="javax.faces" name="jsf.js"/> </h:head> <h:body> <f:view> <h1>JavaBeat JSF 2.2 Examples</h1> <h2>JSF2 UI Remove Example</h2> <h:form prependId="false"> <ui:remove> <h:outputText value="#{indexBean.evaluatedMessage}"></h:outputText> </ui:remove> <!-- <h:output value="#{indexBean.evaluatedProperty}"/> --> </h:form> </f:view> </h:body> </html>
3. The Deployment Descriptor
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" metadata-complete="true"> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2 </description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <param-name>javax.faces.application.CONFIG_FILES</param-name> <param-value>/WEB-INF/faces-config.xml</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> </web-app>
4. Faces Configuration File
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2"> <application> <resource-bundle> <base-name>net.javabeat.jsf.application</base-name> <var>msg</var> </resource-bundle> </application> </faces-config>
5. JSF 2 UI Remove Demo
The below snapshot shows you the impact of using ui:remove Tag for preventing execution and rendition of undesired jsf components.
- As you’ve noted the two output components aren’t rendered, cause the first one has been using the ui:remove and the second one has been using the XML comments.
- But if you’ve looked at the JVM console, you are almost getting a message from that commenting component using the XML comments.
- The point that you have to get it, is that the ui:remove preventing the evaluation of the JSF expressions and the rendition. Meanwhile the XML comments has evaluated the jsf expression without rendering the component.
6. Console Messages
The message that you will get once you are executing the sample program is as follow
Even you're commenting out my associated output component, however it executes the getEvaluatedProperty.Using ui:remove will cause this expression to be not executed[wpdm_file id=44]