Thursday, October 7, 2010

JSF and Struts framework for error control and package handling



In the struts, the commonly used global error control mode is to build a baseAction, in its execute method, complete front operations to return method of dispatch by the try ... ... catch ... ... catch error, realize the error of the control and display. A typical BaseAction examples are:

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 (500, 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:", ex);

forwardPage = processBaseException (request, mapping, ex);

) Catch (Throwable ex) (

log.error ("Error:", 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 ());

)

... ...

)

Used as JSF managed bean, JSP page directly by calling managed bean method in the completion of data exchange, not the same as the struts through the capture dispatch operation to complete throws exception error handling (because there is no dispatch method), seems to jsf do not support the global error handling.

If you throw in the managed bean in an exception (here AppException), look at the console log, we can see that the error is from an ActionListener implementations thrown (for myfaces, here is ActionListenerImpl), reference jsf lifecycle processes, methods 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 should be different according to the framework to determine whether an instance of the program throws the error manually

FacesUtils.addErrorMessage (event.getComponent (). GetClientId (facesContext),

cause.getMessage ());}

else (

throw (AbortProcessingException) cause;

)

) Catch (RuntimeException e) (

throw new FacesException ("Error calling action method of component with id" +

event.getComponent (). getClientId (facesContext), e);

)

NavigationHandler navigationHandler = application.getNavigationHandler ();

navigationHandler.handleNavigation (facesContext, fromAction, outcome);

/ / Render Response if needed

facesContext.renderResponse ();

)

)

Listener configuration, faces-config-application.xml:

Code

org.springframework.web.jsf.DelegatingVariableResolver

resources.application

en

org.snailportal.webframework.listener.GlobalActionListener

This, developers need only managed bean inside the action and based on business needs based on Lei Xing Zhi Ding throw the Exception instance, You BaseAction and ActionListener Wancheng the wrong package, Chu Li, and transmits it gave Qiantai 杩涜 that reveals less developed code, improving maintainability of the framework.







相关链接:



Congratulations to the formal establishment of the Pan-micro Jinan Branch



're Renting Buy A House In The Ascendant With



3G2 to WMV



How to sync iphone ringtone on mac



YUV to AVI



Introduction And Context Servlet Container



ASF Converter



Articles about Games Simulation



Introduction Games Sports



Premier Search Or LOOKUP Tools



The world's richest man Bill Gates: The BATTLE of retirement started Empire



Switch to the best period of 2 to 3 years



Test emergency response capacity of cities: urban emergency on the road



50% stake in MSN China, the main message would be easy to pass and then mad



No comments:

Post a Comment