Tuesday, September 16, 2008

Annotations and Interceptors in struts2

The power of annotation is now available in struts2. All your validations can be made easy just by annotating the field, which you want to validate. Example, you can make a null check, email validation and some more built in validation annotators.

You can get the power of annotation in your application just , by annotating the getter method of the respective field in the JSP page.
Example
If I have a field name username in my struts2 JSP page, and I want to make a validation on it(If value is not entered, a message should be thrown) then, just annotate the action class using @validation and you can do the validation for textfield like

@RequiredStringValidator(message="Enter username ")
public String getUsername() {
return username;
}
And in the JSP page you can get the message in the same way, as how you get when you add a fielderror.

You can also invoke a method, before invoking your action class method, after execution of your action class method and before returning result from action method, by using @before, @after, and @before result annotations. For this to work you have to configure interceptor stack in your struts.xml .

<interceptors>

<interceptor name="annotationWorkFlow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"/>
<interceptor-stack name="demostack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="annotationWorkFlow"/>
/span>interceptor-stack>
interceptors>
<default-interceptor-ref name="demostack" />
If you add the above line within action tag then the interceptor tag will be applicable for that particular action alone. If you add the line out of the action tag then all the actions will pass through the set of interceptors.

You can also define your own interceptor classes.
For that your interceptor class should implement the interceptor interface. The example class is as shown below. Your request will pass through this interceptors in the order specified in the struts.xml file .

public class TestInterceptor implements Interceptor{

public String intercept(ActionInvocation next) throws Exception {
long t1 = System.currentTimeMillis();
String s= next.invoke();
long t2 = System.currentTimeMillis();
System.out.println("From Interceptor Action "+next.getAction().getClass().getName()+" took "+(t2-t1)+" millisecs");
return s;
}


public void destroy() {
}


public void init() {
// TODO Auto-generated method stub

}
}
Here the interceptor class just calculates the time taken to fullfill the request. The ActionInvoactions , invoke method will invoke the corresponding method in the action class.

To access the request and response within the Interceptor class, you need to getInvocationContext() and you need to get the HttpServletRequest and HttpServletResponse like this
HttpServletRequest request=(HttpServletRequest)next.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);

HttpServletResponse response=(HttpServletResponse)next.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);

Thus annotations and Interceptors gives flexibility and ease of development of our application.

No comments: