Struts 2 prepare Interceptor

In order to implement prepare interceptor in the struts web application, the class must implement a Preparable interface and override the prepare method along with the execute method. While executing prepare interceptor, prepare() method executes first before execute() method.

Struts prepare Interceptor Example:

To get input from the user create the index.jsp file:

The index.jsp page is created to take input from the user. The user has to enter the name and password and then after clicking on the login button, it will redirect to the next resource. Create two text field to enter name and password along with the label to enter name and password and one submit button on the form with the link to Login class.

<%@ taglib uri="/struts-tags" prefix="s" %>


<s:form action="login">
<s:textfield name="name" label="Enter the Name"></s:textfield>
<s:password name="password" label="Enter the Password"></s:password>
<s:submit value="login"></s:submit>
</s:form>

Create success.jsp file to denote success:

The success.jsp page displays Welcome and username on success. When the execute() method return success, this page executes.

<%@ taglib uri="/struts-tags" prefix="s" %>


Welcome, <s:property value="name"/>

Create error.jsp file to denote failure:

The error.jsp page displays Sorry, username or password error! on failure. When the execute() method returns an error, this page executes. It also includes index.jsp to try again to enter name and password.

Sorry, username or password error!
<jsp:include page="index.jsp"></jsp:include>

Create the action class Login.java:

This is the action class. It contains two fields name and password with their getter and setters. It contains the execute method which includes code for actual business logic and prepares method which includes code for preparation logic to be executed at the beginning. On success when the password matches it will go to the success.jsp page otherwise on error it will redirect to error.jsp.

import com.opensymphony.xwork2.Preparable;


public class Login implements Preparable
{
private String name,password;


public String getName()
 {
return name;
}


public void setName(String name) 
{
this.name = name;
}


public String getPassword() 
{
return password;
}


public void setPassword(String password)
 {
this.password = password;
}
@Override
public void prepare() throws Exception 
{
System.out.println("Write the preparation logic here......");
}
public String execute()
{
System.out.println("Write the actual business logic here.....");
           if(password.equals("12345"))
          {
return "success";
          }
         else
         {
return "error";
         }
}
}

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

The web.xml file defines how elements are processed. The entry of FilterDispatcher is done in the web.xml file. This file is created in WebContent->WEB-INF folder. /* specifies all urls will be parsed. This task is done by struts filter.

<?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>StrutsPrepInteceptor</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>  
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
   </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Construct struts.xml file:

In the struts.xml file, make the entry of the interceptor. Also, specify the action class Login and link for it and result pages. The result determines what browser will display after the execution of the action. Results have optional names like success and error.

<?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"></constant>
<package name="abc" extends="struts-default" >


<action name="login" class="Login">
<interceptor-ref name="params"></interceptor-ref>
<interceptor-ref name="prepare"></interceptor-ref>
<result name="success" type="dispatcher">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>


</package>
</struts> 

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 two labels for entering the name and password and the login button. Enter the name and password “12345”. Then click on the login button.

Struts prepare Interceptor

As the name is Sanket and the password is 12345, it will return success. The prepare() method executes first and displays Write the preparation logic here...... Then execute() method executes and return Write the actual business logic here..... As the name and password are correct, it will redirect to success.jsp displaying Welcome, Sanket.

Struts prepare Interceptor

Again run the application. This time enter any name and wrong password 12345678.

Struts prepare Interceptor

As the password is wrong, it will return an error and redirect to error.jsp and display a message Sorry, username or password error! and again display text field to enter name and password.

Struts prepare Interceptor