Error control and encapsulation processing for JSF and struts frameworks

Source: Internet
Author: User
Tags error handling getmessage throwable

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.