Interceptor implementing SessionAware interface

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Interceptor implementing SessionAware interface

saravana2906@gmail.com
Hi,
I am trying to create custom interceptor which implements SessionAware interface to handle session management logic so that cross-cutting concern is not implemented Action class which is concentrating on core concern.
setSession() method is not called even after implementing sessionAware and even in struts website , suggestions given to implement in action class and not in Interceptor.

I went through some question in SO and it suggested to add servletconfig interceptor in struts.xml .But I don't know why to add it even though it's already part of default stack.

Struts.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
       <package name="default" extends="struts-default">
     <interceptors>
    <interceptor name="sessionmanage" class="com.contacts.interceptors.SessionInterceptor" />
    <interceptor-stack name="appStack">  
         <interceptor-ref name="defaultStack"/>
         <interceptor-ref name="sessionmanage"/>
    </interceptor-stack>
    </interceptors>
    <default-interceptor-ref name="appStack"></default-interceptor-ref>
            <action name="">
            <result>/signup.jsp</result>
        </action>
        <action name="signup">
           <result>/signup.jsp</result>
        </action>
        <action name="signup_action" class="com.contacts.actions.SignupAction" method="execute">
        <result name="input">/signup.jsp</result>
            <result>/succcess.jsp</result>
        </action>
   </package>
</struts>

SessionInterceptor.java

package com.contacts.interceptors;

import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class SessionInterceptor extends AbstractInterceptor  implements SessionAware  {
        SessionMap<String, Object> session;

        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        public String intercept(ActionInvocation arg0) throws Exception {
                // TODO Auto-generated method stub
                //preprocessing
        System.out.println("Pre-processing");
                        arg0.invoke();
                        System.out.println("Post-Processing");
                return "SUCCESS";
        }

        @Override
        public void setSession(Map<String, Object> session) {
                // TODO Auto-generated method stub
                System.out.println("Set Session Method");
                this.session=(SessionMap<String,Object>) session;
               
        }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Interceptor implementing SessionAware interface

Yasser Zamani-2


On 12/4/2017 3:00 AM, [hidden email] wrote:
> setSession() method is not called even after implementing sessionAware

ServletConfigInterceptor calls setSession but only on action, not on
previous or next interceptors:

         if (action instanceof SessionAware) {
             ((SessionAware) action).setSession(context.getSession());
         }

I think you can rewrite your interceptor as below instead (like
ServletConfigInterceptor):

public class SessionInterceptor extends AbstractInterceptor  {
        SessionMap<String, Object> session;

        @Override
        public String intercept(ActionInvocation arg0) throws Exception
  {
session = arg0.getInvocationContext().getSession();
                //...
        }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]