Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Custom Interceptor


implementing the Interceptor interface in a class and overriding its three life cycle method.

For creating the custom interceptor, Interceptor interface must be implemented. It has three methods:

  1. public void init() It is invoked only once and used to initialize the interceptor.
  2. public String intercept(ActionInvocation ai) It is invoked at each request, it is used to define the request processing logic. If it returns string, result page will be invoked, if it returns invoke() method of ActionInvocation interface, next interceptor or action will be invoked.
  3. public void destroy() It is invoked only once and used to destroy the interceptor.

Interceptor can change the flow of the application by returning the string.


Example to create custom interceptor in struts 2

In this example, we are going to create custom interceptor that converts request processing data into uppercase letter.

You need to follow 2 steps to create custom interceptor

  1. Create an interceptor (must implement Interceptor interface)
  2. Define the entry of interceptor in the struts.xml file

1) Create an interceptor (must implement Interceptor interface)

By this interceptor, we are converting the name property of action class into uppercase letter.

The getStack() method of ActionInvocation returns the reference of ValueStack.

We are getting the value set in the name property by findString method of ValueStack.

The set method of ValueStack sets the name property by the specified value. In such case, we are converting the value of name property into uppercase letter and storing it into the valuestack.

The invoke method of ActionInvocation returns the information of next resource.

MyInterceptor.java

  1. package com;
  2. import com.opensymphony.xwork2.ActionInvocation;
  3. import com.opensymphony.xwork2.interceptor.Interceptor;
  4. import com.opensymphony.xwork2.util.ValueStack;
  5. public class MyInterceptor implements Interceptor{
  6.     public void init() {}
  7.     public String intercept(ActionInvocation ai) throws Exception {
  8.         ValueStack stack=ai.getStack();
  9.         String s=stack.findString(“name”);
  10.         stack.set(“name”,s.toUpperCase());
  11.         return ai.invoke();
  12.     }
  13.     public void destroy() {}
  14. }

2) Define the entry of interceptor in the struts.xml file

To define the interceptor, we need to declare an interceptor first. The interceptors element of package is used to specify interceptors. The interceptor element of interceptors is used to define the custom interceptor. Here, we are defining the custom interceptor by upper.

The interceptor-ref subelement of action specifies the interceptor that will be applied for this action. Here, we are specifying the defaultstack interceptors and upper interceptor.

struts.xml

  1. <?xml version=“1.0” encoding=“UTF-8” ?>
  2. <!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts
  3. Configuration 2.1//EN” “http://struts.apache.org/dtds/struts-2.1.dtd”>
  4. <struts>
  5. <package name=“abc” extends=“struts-default”>
  6. <interceptors>
  7. <interceptor name=“upper” class=“com.MyInterceptor”></interceptor>
  8. </interceptors>
  9. <action name=“login” class=“com.Login”>
  10. <interceptor-ref name=“defaultStack”></interceptor-ref>
  11. <interceptor-ref name=“upper”></interceptor-ref>
  12. <result>welcome.jsp</result>
  13. </action>
  14. </package>
  15. </struts>

Other Required Resources

The other required resources are

  • index.jsp
  • Login.java
  • welcome.jsp

1) Create form to get input (index.jsp)

index.jsp

This jsp page creates a form using struts UI tags. It receives name from the user.

  1. <%@ taglib uri=“/struts-tags” prefix=“s” %>
  2. <s:form action=“login”>
  3. <s:textfield name=“name” label=“Name”></s:textfield>
  4. <s:submit value=“login”></s:submit>
  5. </s:form>

2) Create the action class

It is the simple action class containing name property with its setter and getter methods.

Login.java

  1. package com;
  2. public class Login {
  3. private String name;
  4. public String getName() {
  5.     return name;
  6. }
  7. public void setName(String name) {
  8.     this.name = name;
  9. }
  10. public String execute(){
  11.     return “success”;
  12. }
  13. }

3) Create view component

This jsp page displays the name input by the user.

welcome.jsp

  1. <%@ taglib uri=“/struts-tags” prefix=“s” %>
  2. Welcome, <s:property value=“name”/>

Comments

comments