• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Struts 2 Interceptors

May 30, 2012 By j2eereference Leave a Comment

Interceptors can execute code before and after an Action is invoked .Interceptors sits in between the controller and the action ,and invoked by controller before and after invoking an action. It can perform actions such as Logging,Validation,File upload etc.

In Struts 2 , interceptors are called one after the other in a configured order for each invocation of an action class. The interceptors have an opportunity to interact with the input from user, and to interact with the action after it completes it’s processing.

The main method in an interceptor is the intercept() method. This method is passed an ActionInvocation object, which contains the action, and the context for the action. The intercept method returns a String that maps to a “result” in struts.xml file.

Let’s understand the use of interceptor with an example. In this example we are checking whether the user is already logged in or not before going to the action class . Assume that first time when a user logs in we are storing the user details in session attribute called user.

 

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.j2eereference.interceptor;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class SessionCheckInterceptor implements Interceptor {
 
private static final long serialVersionUID = 1L;
 
  @Override
  public String intercept(ActionInvocation invocation) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession(true);
// Checking whether the user Object that had set at login time is null or not
 
if (session.getAttribute("user") == null) {
return "invalidsession";
}
return invocation.invoke(); // If user already logged in
}
 
@Override
public void destroy() {              
System.out.println("Destroying Interceptor");
}
 
@Override
public void init() {              
System.out.println("Initialising Interceptor");
     }
}

Now lets see the configuration in struts.xml.

 

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
-----
---
 
<interceptors>
<interceptor name="SessionCheck" class="com.action.SessionCheckInterceptor">
</interceptor>
<interceptor-stack name="newStack">
<interceptor-ref name="SessionCheck"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
 
<!—-If we need to apply the interceptor for all the action classes-->
 
<default-interceptor-ref-name="checkSessionStack"/><global-results>
<result name="invalidSession">/login.jsp</result></global-results>
 
<!-- If we need to apply interceptor for a particular action -->
 
<action name="initAction" class="com.action.LoginAction"
method="init">
<interceptor-ref name="newStack">
</interceptor-ref>
<result name="invalidsession">/login.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
 
---
---
</struts>


Filed Under: Struts

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.