In struts, the commonly used global error control mode is to build a baseaction, in its Execute method to complete the foreground return method dispatch operation, and by Try......catch ... Capture program errors, implement error control and presentation. A typical baseaction example is as follows:
Code
public Actionforward Execute (actionmapping mapping, actionform form,
HttpServletRequest request, HttpServletResponse response) {
...
Actionforward forwardpage = null;
try {
String parameter = Mapping.getparameter ();
if (parameter = = null) {
String message = Messages.getmessage ("Dispatch.handler", Mapping.getpath ());
Response.senderror (for message);
return null;
}
String name = Processreqcode (request.getparameter (parameter));
Forwardpage = dispatchmethod (mapping, form, request, response, name);
} catch (Baseexception ex) {
if (log.isdebugenabled ())
Log.debug ("Error occurred:", ex);
Forwardpage = processbaseexception (request, mapping, ex);
} catch (Throwable ex) {
Log.error ("Error occurred:", ex);
Actionmessages errors = new Actionmessages ();
Bytearrayoutputstream ostr = new Bytearrayoutputstream ();
Ex.printstacktrace (New PriNtstream (OSTR));
Errors.add ("Org.apache.struts.action.GLOBAL_MESSAGE", New Actionmessage
(ostr.tostring ());
Saveerrors (request, errors);
Forwardpage = Mapping.findforward ("Syserror");
Output.setstatus ("fail");
Output.seterror (Ex.getmessage ());
}
...
}
Since JSF uses the managed bean,jsp page to complete data interaction directly by calling methods in the managed bean, Instead of doing the wrong thing like struts by capturing the exception thrown by the dispatch operation (because there is no dispatch method at all), it seems that JSF does not support global error handling at all.
If you throw a exception in the managed bean (this is appexception), look at the log of the console and see that the error is actually thrown from the implementation of a ActionListener (for Myfaces, Here is the Actionlistenerimpl), referring to the JSF lifecycle process, the method comes out:
Code
public class Globalactionlistener extends Actionlistenerimpl {
public void Processaction (ActionEvent event ) throws Abortprocessingexception {
Facescontext facescontext = Facescontext.getcurrentinstance ();
Application application = Facescontext.getapplication ();
Actionsource Actionsource = (actionsource) event.getcomponent ();
Methodbinding methodbinding = Actionsource.getaction ();
String fromaction = null; String outcome = null;
if (methodbinding! = null) {
Fromaction = methodbinding.getexpressionstring ();
try {
outcome = (String) methodbinding.invoke (facescontext, null);
catch (Evaluationexception e) {
Throwable cause = E.getcause ();
if (cause!) = null && cause instanceof appexception) {
//It is necessary to determine whether an instance is a manually thrown error in a program
, depending on the framework Facesutils.adderrorme Ssage (Event.getcomponent (). Getclientid (Facescontext),
Cause.getmessage ());
else {
Throw (abortprocessingexception) cause;
} catch (RuntimeException e) {
throw new facesexception ("Error calling action method, component with I D "+
Event.getcomponent (). Getclientid (Facescontext), E);
}
Navigationhandler Navigationhandler = Application.getnavigationhandler ();
Navigationhandler.handlenavigation (Facescontext, fromaction, outcome);
//Render Response if needed
Facescontext.renderresponse ();
}
}