jsf - Not redirecting to error page on exception -
i trying navigate error page if exception occurred. have defined:
<error-page> <error-code>500</error-code> <location>/error.jspx</location> </error-page>
in web.xml
. have tried servlet:
<servlet> <servlet-name>errorhandler</servlet-name> <servlet-class>web.servlet.errorhandler</servlet-class> </servlet> <servlet-mapping> <servlet-name>errorhandler</servlet-name> <url-pattern>/errorhandler</url-pattern> </servlet-mapping> <error-page> <error-code>500</error-code> <location>/errorhandler</location> </error-page>
but neither navigating error.jspx
nor errorhandler
servlet called.
to test error handling have tried throw new exception("test");
both of constructor of managed bean , actionlistener
. printing exception in console redirection not happening.
i have tried with: <exception-type>java.lang.exception</exception-type>
instead of <error-code>500</error-code>
, no luck. how can invoke servlet or navigate page whenever exception occurred anywhere constructor or action
/actionlistener
?
i not know if serve you. have handler error defined follows.
in "faces-config.xml"
<factory> <exception-handler-factory> com.mypackage.global.datexceptionhandlerfactory </exception-handler-factory> </factory>
and 2 classes
import javax.faces.context.exceptionhandler; import javax.faces.context.exceptionhandlerfactory; public class datexceptionhandlerfactory extends exceptionhandlerfactory { private exceptionhandlerfactory parent; // injection handles jsf public datexceptionhandlerfactory(exceptionhandlerfactory parent) { this.parent = parent; } //create own exceptionhandler @override public exceptionhandler getexceptionhandler() { exceptionhandler result = new datexceptionhandler(parent.getexceptionhandler()); return result; } }
second class
import java.util.iterator; import javax.faces.facesexception; import javax.faces.application.navigationhandler; import javax.faces.context.exceptionhandler; import javax.faces.context.exceptionhandlerwrapper; import javax.faces.context.facescontext; import javax.faces.context.flash; import javax.faces.event.exceptionqueuedevent; import javax.faces.event.exceptionqueuedeventcontext; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; public class datexceptionhandler extends exceptionhandlerwrapper { private static log log = logfactory.getlog(datexceptionhandler.class); private exceptionhandler wrapped; public string error = "n"; public string geterror() { return error; } public void seterror(string error) { this.error = error; } public datexceptionhandler(exceptionhandler wrapped) { this.wrapped = wrapped; } @override public exceptionhandler getwrapped() { return wrapped; } @override public void handle() throws facesexception { iterator iterator = getunhandledexceptionqueuedevents().iterator(); while (iterator.hasnext()) { exceptionqueuedevent event = (exceptionqueuedevent) iterator.next(); exceptionqueuedeventcontext context = (exceptionqueuedeventcontext)event.getsource(); throwable throwable = context.getexception(); facescontext fc = facescontext.getcurrentinstance(); try { flash flash = fc.getexternalcontext().getflash(); // put exception in flash scope displayed in error // page if necessary ... flash.put("errordetails", throwable.getmessage()); system.out.println("the error put in flash: " + throwable.getmessage()); navigationhandler navigationhandler = fc.getapplication().getnavigationhandler(); navigationhandler.handlenavigation(fc, null, "components/errorhandler.xhtml?faces-redirect=true"); fc.renderresponse(); } { iterator.remove(); } } // let parent handle rest getwrapped().handle(); } }
and errorhandler.xhtml show error
<?xml version='1.0' encoding='utf-8' ?> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <link type="text/css" rel="stylesheet" href="#request.contextpath}/css/default.css" /> <title>#{bundle['guessnumber.error_page.title']}</title> </h:head> <h:body> <div class="highlighted errormessage"> <h:outputtext escape="false" value="#{bundle['guessnumber.error_page.content']}"/> </div> <br/><br/> <div class="errordetails"> error details: <br/> #{flash.keep.errordetails} </div> </h:body> </html>
Comments
Post a Comment