Struts 2 Custom Interceptor Example Tutorial


In struts 2, we can make the custom interceptor by executing the Interceptor interface in a class and superseding its three life cycle method.

For making the custom interceptor, Interceptor interface must be actualized. It has three methods:

  1. public void init() It is conjured just once and used to instate the interceptor.
  2. public String intercept(ActionInvocation ai) It is conjured at each solicitation, it is utilized to characterize the solicitation handling rationale. In the event that it returns string, result page will be conjured, on the off chance that it returns summon() technique for ActionInvocation interface, next interceptor or activity will be invoked.
  3. public void destroy() It is summoned just once and used to decimate the interceptor.

Example to make custom interceptor in struts 2

In this model, we will make custom interceptor that changes over solicitation preparing information into capitalized letter.

You need to pursue 2 steps to make custom interceptor

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

1) Create an interceptor (must execute Interceptor interface)

By this interceptor, we are changing over the name property of activity class into capitalized letter.

The getStack() technique for ActionInvocation restores the reference of ValueStack.

We are getting the esteem set in the name property by findString technique for ValueStack.

The set technique for ValueStack sets the name property by the predefined esteem. In such case, we are changing over the estimation of name property into capitalized letter and putting away it into the valuestack.

The invoke strategy for ActionInvocation restores the data 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.   
  6. public class MyInterceptor implements Interceptor{  
  7.   
  8.     public void init() {}  
  9.     public String intercept(ActionInvocation ai) throws Exception {  
  10.         ValueStack stack=ai.getStack();  
  11.         String s=stack.findString("name");  
  12.           
  13.         stack.set("name",s.toUpperCase());  
  14.         return ai.invoke();  
  15.     }  
  16.     public void destroy() {}  
  17. }  

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

To characterize the interceptor, we have to pronounce an interceptor first. The interceptors component of bundle is utilized to indicate interceptors. The interceptor component of interceptors is utilized to characterize the custom interceptor. Here, we are characterizing the custom interceptor by upper.

The interceptor-ref subelement of activity indicates the interceptor that will be connected for this activity. Here, we are determining 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.   
  7. <interceptors>  
  8. <interceptor name="upper" class="com.MyInterceptor"></interceptor>  
  9. </interceptors>  
  10.   
  11. <action name="login" class="com.Login">  
  12. <interceptor-ref name="defaultStack"></interceptor-ref>  
  13. <interceptor-ref name="upper"></interceptor-ref>  
  14. <result>welcome.jsp</result>  
  15. </action>  
  16.   
  17. </package>  
  18. </struts>      

Other Required Resources

The other required assets are

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

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

index.jsp

This jsp page makes a structure utilizing struts UI labels. It gets name from the client.

  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 activity class

It is the straightforward activity class containing name property with its setter and getter methods.

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

3) Create see component

This jsp page shows the name contribution by the user.

welcome.jsp
  1. <%@ taglib uri="/struts tags" prefix="s" %>  
  2. Welcome, <s:property value="name"/>  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition