Implementation of Struts2.0 Interceptor Interface

Implementation of Struts2.0 Interceptor Interface

Previous Home Next

 

An interceptor is an object which provides some services to the action before execution of action or after the result is render. Functionality of interceptor is defined by Interceptor Interface. Which is implemented by all interceptors? This interface provides lifecycle method of interceptor.

The lifecycle method of an interceptor:

init():-It is invoked only one just after an interceptor is created. It is used to initialize interceptor.

public void init();

intercept ():-This method is invoked each time a request for the action on which intercept is apply preprocessing or post processing logic.

public String intercept( ActionInvocation  ai );

destroy():-This method is invoked only once just before an interceptor are unloaded.

public void destroy(); 


Directory Structure of Interceptor Interface Example in Struts 2.0 Using MyEclipse IDE




index.jsp

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

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

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="demo">
<result-types>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
</result-types>
<interceptors>
<interceptor name="paramSetter" class="mypack.MyInterceptor"/>
</interceptors>
<action name="login" class="mypack.LoginAction">
<interceptor-ref name="paramSetter"/>
<result name="success">/hello.jsp</result>
<result name="failure">/relogin.jsp</result>
</action>
</package>
</struts>

MyInterceptor.java

package mypack;
import java.util.Enumeration;
import javax.servlet.ServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;

public class MyInterceptor implements Interceptor {
	public void destroy() {	}
	public void init() { }
	public String intercept(ActionInvocation ai) throws Exception {
		ServletRequest request=ServletActionContext.getRequest();
		ValueStack vs=ai.getStack();
		Enumeration<String> e=request.getParameterNames();
		while(e.hasMoreElements())
		{
			String pName=e.nextElement();
			String pValue=request.getParameter(pName);
			System.out.println(pName+"\t"+pValue);
			vs.setValue(pName,pValue );
		}String str=ai.invoke();
		return str;
	}

}

LoginAction.java

package mypack;
public class LoginAction {
	private String name,password;
	public String execute(){
		if(name.startsWith("a")&&password.startsWith("b"))
			return "success";
		else
			return "failure";		
	}

	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;
		}

}

hello.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<b>Welcome, <s:property value="name"/></b>

relogin.jsp

<b>Invalid UserName and Password !</b>
<jsp:include page="index.jsp"/>


Output





 

Previous Home Next