struts2 阻截配置

struts2 拦截配置


    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
   
    <package name="com" extends="struts-default">
        <interceptors>   
            <interceptor name="sessionout" 
             class="com.joyveb.jlbos.interceptor.SessionInterceptor"></interceptor>
             <!-- 拦截器栈  -->
             <interceptor-stack name="mydefault"> 
                <interceptor-ref name="defaultStack" />  
                <interceptor-ref name="sessionout"/> 
            </interceptor-stack> 
        </interceptors> 
       
        <default-interceptor-ref name="mydefault"/>
       
        <global-results>
            <result name="login">/index.jsp</result>
        </global-results>

 

 

 

 

 

package com.joyveb.jlbos.interceptor;

import java.util.Map;

import com.joyveb.jlbos.action.LoginAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class SessionInterceptor extends AbstractInterceptor {

     @Override 
        public String intercept(ActionInvocation actionInvocation) throws Exception { 
            ActionContext ctx = ActionContext.getContext(); 
            Map<String,Object> session = ctx.getSession(); 
            Action action = (Action) actionInvocation.getAction(); 
            if (action instanceof LoginAction) { 
                return actionInvocation.invoke(); 
            } 
            String userName = (String) session.get("userName"); 
            if (userName == null) { 
                return Action.LOGIN; 
            } else { 
                return actionInvocation.invoke(); 
            } 
        } 

}