<?xml version="1.0" encoding= "UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name ="struts.devMode" value= "true"></constant >
<constant name= "struts.enable.DynamicMethodInvocation" value= "true"></constant >
<package name ="test" namespace="/" extends= "struts-default">
<!-- 定义一个权限控制拦截器 -->
<interceptors >
<interceptor name ="authority" class= "com.cola.interceptor.AuthorizationInterceptor" ></interceptor >
</interceptors >
<!-- 定义一个全球结果集处理方式,所有经过拦截器的action都会按下面处理跳转 -->
<global-results >
<result name ="login">/login.jsp</ result>
</global-results >
<action name ="test" class= "com.cola.action.TestAction" method ="execute">
<result name ="success" type= "dispatcher">/test.jsp</result >
<!-- 使用拦截器 -->
<interceptor-ref name= "defaultStack"></interceptor-ref >
<interceptor-ref name= "authority"></interceptor-ref >
</action >
</package >
<include file ="struts2.xml"></ include>
</struts>
--------------------------------------------------
package com.cola.interceptor;
import java.util.Map;
import com.cola.bean.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
@SuppressWarnings("all" )
public class AuthorizationInterceptor implements Interceptor{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public String intercept(ActionInvocation ai) throws Exception {
Map<String,Object> session = ai.getInvocationContext().getSession();
User user = (User) session.get( "user");
if( user!= null){
return ai.invoke();
}
return Action. LOGIN;
}
}