The purpose of this post is to explain, How we can use struts 2 interceptor to bring exception messages into the view screen of your web application and be aware the user about those in meaning full way. There may be several ways of implementing this kind of functionality. My major purpose of this technique is to keep developer significantly free for taking exception messages to the view screen, and let the interceptor to do this instead, once We have added this feature into our system.
This is very straight forward method. Each struts 2 action is invoked from a interceptor's intercept method and catch the exception with in intercept method and send the message to the view. I am going to explain this technique for struts 2 action invocation via AJAX request. I named the interceptor as ExceptionFlowInterceptor. Let's see, How configure the interceptor in
struts.xml file.
<package name="autoac" extends="struts-default json-default" namespace="/secure"> <interceptors> <interceptor name="exceptionFlowInterceptor" class="com.semika.autoac.web.interceptors.ExceptionFlowInterceptor"></interceptor> <interceptor-stack name="autoac-stack"> <interceptor-ref name="exceptionFlowInterceptor"></interceptor-ref <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="autoac-stack"></default-interceptor-ref> </package>
Better to give little explanation about the above configuration. I have declared a package called "autoac" which extends struts-default and json-default packages. Since I want to invoke actions in autoac package with ajax request, I have extended autoac package from json-default package. To know further about that, please read about struts 2 json plugin.
I have configured the interceptor so that each action in /secure name space is undergone interceptor's pre-processing and post processing.
Next, We will see the code for our interceptor. I am not going to put the whole source here. I just put, How intercept method behaves.
@Override public String intercept(ActionInvocation invocation) throws Exception { String result = null; try{ result = invocation.invoke(); } catch(Exception e) { ValueStack vs = invocation.getStack(); if (e.getMessage() == null) { vs.setValue("errorMessage", "Internal system faliure.Please contact system administrator."); } else { vs.setValue("errorMessage", e.getMessage()); } vs.setValue("status", "fail"); result = "json"; } return result; }
I guess, You have already got the point. I have invoked the action from intercept method and within in try catch block. Any of the exceptions raised after the action invocation, will be caught here. In case of an exception, You can catch the exception and send it into the view in nice way. In my case, I am handling status field at action's model and update that field accordingly. That is up to you to handle this point.
I have obtained the value stack and update the status field as an error. Struts 2 json plugin serializes the whole action and resulting json object which can be accessed with Javascript.