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: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to