Struts 2 params Interceptor

Parameters Interceptor is also known as params interceptor implemented in struts application by implementing Interceptor interface. We have to override all three life cycle methods. The Interceptor interface itself provides methods to implement params interceptor. Parameters defined with params tag are:

  • ordered: It is used to set property setter behaviour as top-down.
  • paramNameMaxLength: It is used to set the maximum length for parameters.
  • excludeParams: It is used to specify the names of the parameters which are not allowed.
  • acceptParamNames: It is used to list the names of parameters that are accepted.

params interceptor life cycle methods are:

  1. public void init(): This method is used to initialize the interceptor. It is called by the web container. It is called only once.
  2. public String intercept(ActionInvocation ai): This method is invoked for each request. Request processing logic is defined with the help of this method. If invoke() method is returned by this method, the next action will be called. If the method returns a string then the result page is called.
  3. public void destroy(): The main use of this method is to destroy the interceptor. It is called only once. It performs a clean-up process.

Struts params Interceptor Example:

Create the Interceptor that implements Interceptor interface:

First, create a java class in a package that implements the Interceptor interface and override all the 3 life cycle methods init(), intercept() and destroy().

init() method is used to initialize the interceptor.

intercept() method provides logic to be executed.

The next resource is then obtained with invoke() method

destroy() method is at last used for releasing the resources allocated.

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;


@Override
public void init() 
{
// TODO Auto-generated method stub
}
@Override
public String intercept(ActionInvocation invocation)throws Exception 
{
 String result = invocation.invoke();
    return result;
  }
@Override
public void destroy() 
{
// TODO Auto-generated method stub

}


}

Construct struts.xml file with the entry of Interceptor in it:

In the struts.xml file, make the entry of the interceptor. We are specifying interceptor by the name MyInterceptor. Also, we have specified the interceptor-ref name as params in the action tag.

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts  
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>
   <constant name = "struts.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">
      <interceptors>
         <interceptor name = "myinterceptor"
            class = "MyInterceptor" />
      </interceptors>


      <action name = "hello" 
         class = "HelloWorld" 
         method = "execute">
         <interceptor-ref name = "params"/>
         <interceptor-ref name = "myinterceptor" />
         <result name = "success">/Helloworldname.jsp</result>
      </action>
   </package>
</struts>

In order to get input from the user create index.jsp file:

Create index.jsp file in WebContent folder for taking name as a input from the user and the Submit button for form submission. Crate a form and place one textfield and a submit button in it.

<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
   pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd">


<html>
   <head>
      <title>Hello World Example</title>
   </head>
   
   <body>
<h1>Hello World With Params Interceptor</h1>
<form action = "hello">
         <label for = "name">Enter your name</label><br/>
         <input type = "text" name = "name"/>
         <input type = "submit" value = "Submit"/>
      </form>
   </body>
</html>

Create Helloworldname.jsp file to denote success:

Create JSP file Helloworldname.jsp in the WebContent folder. On success it will redirect to Helloworldname.jsp

Helloworldname.jsp

<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<html>
   <head>
      <title>Hello World</title>
   </head>   
   <body>
      Welcome,  <s:property value = "name"/>
   </body>
</html>

Create action class HelloWorld.java:

Action class HelloWorld.java contains name field and it’s getter, setters with execute() method which returns Success string.

import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
   private String name;
   public String execute() throws Exception {
      return "success";
   }  
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

web.xml file is created inside WEB-INF folder in WebContent folder:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts 2</display-name>
   
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>


   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

Output:

In order to run the application, right-click on the project -> Click on the option Run As -> then select Run on Server. It will show a label for entering the name and the submit button.

Struts params Interceptor

Enter the name and click on the Submit button.

Struts params Interceptor

After Success, it will redirect to Helloworldname.jsp displaying Welcome, Sanket

Struts params Interceptor