Friday, October 22, 2010

Struts2 exception flow interceptor

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">
     <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>

<default-interceptor-ref name="autoac-stack"></default-interceptor-ref>


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.

public String intercept(ActionInvocation invocation) throws Exception {
   String result = null;
       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.

Thursday, October 14, 2010

CSS extend

This is very simple thing that we do not care too much. When we write CSS file, have you knew that, we can extend CSS attributes from one CSS class to a another CSS class. I will explain this using a very simple example.

Suppose you want to have some customized border styles for cells of a table. For some table cells You need only the top border only, and some other cell You need only the top and bottom border only. For this kind of scenario's, We can use CSS extends feature rather adding repeated CSS attributes.

This is the CSS class for table border top.
.border-top {
        border-top: 1px solid #CCCCCC;

Next, You want to have another CSS class for table cells with top and bottom border. If You do not consider about CSS extends, You have to write a CSS class as fallows.
border-top-bottom {
      border-top:1px solid #CCCCCC
      border-bottom:1px solid #CCCCCC;

You can see, You have repeatedly added "border-top: 1px solid #CCCCCC;" line for top-bottom CSS class as well. If you extends 'border-top-bottom' CSS class from 'border-top' CSS class, You do not need to do so.

Now I am going to modify 'border-top' CSS class as fallows.
.border-top,  .border-top-bottom {
      border-top: 1px solid #CCCCCC;

The above CSS class declarations simply says that, 'border-top-bottom' CSS class inherits all the CSS attributes from 'border-top' CSS class. Then You can write 'border-top-bottom' CSS class as fallows.
.border-top-bottom {
      border-bottom: 1px solid #CCCCCC;

You do not need to add 'border-top: 1px solid #CCCCCC;' attribute repeatedly. Further if You want to write CSS class for table cells with border top and right, You can modify 'border-top' CSS class as fallows.
.border-top, .border-top-bottom  .border-top-right{
     border-top: 1px solid #CCCCCC;

See carefully, I have added a comma after 'border-top' CSS class and not after 'border-top-bottom'. The above CSS declaration says that, 'border-top-bottom' and 'border-top-right' both CSS classes inherits CSS attributes from 'border-top' CSS class.