Struts2配置拦截器

=============================java==============
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.zr.pojo.aged.home.ZaghUsers;


public class UtilInterceptor implements Interceptor {

/**
* 实现登录拦截
*/
private static final long serialVersionUID = -532172353763647921L;

public void destroy() {
// TODO Auto-generated method stub

}

public void init() {
// TODO Auto-generated method stub

}

@SuppressWarnings("unchecked")
public String intercept(ActionInvocation invocation) throws Exception {
String methodName=null;
Map map = invocation.getInvocationContext().getParameters();
if (map == null || map.size() == 0) {
System.out.println("0");
} else {
String[] keys = new String[map.size()];
map.keySet().toArray(keys);
for (int i = 0; i < keys.length; i++) {
if (map.get(keys[i]) != null) {
String[] values = (String[])map.get(keys[i]);
for (int j = 0; j < values.length; j++) {
methodName=values[0];
}
}
}
}
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
// 取出名为user的session属性
ZaghUsers user = (ZaghUsers) session.get("userbean");
// 如果没有登陆,返回重新登陆
if (user != null||(methodName!=null&&methodName.equalsIgnoreCase("User.checkUserLogin"))||(methodName!=null&&methodName.equalsIgnoreCase("Home.index"))) {
return invocation.invoke();
} else {
return "/index.jsp";
}
//return invocation.invoke();
}



// @Override
// protected String doIntercept(ActionInvocation invocation) throws Exception {
// ActionProxy proxy=invocation.getProxy();
// String methodName=proxy.getMethod();
// Method method=null;
// method = proxy.getAction().getClass().getMethod(methodName, new Class[0]);
String methodName=proxy.getClass().getMethod(name, parameterTypes);
// // 取得请求相关的ActionContext实例
// ActionContext ctx = invocation.getInvocationContext();
// Map session = ctx.getSession();
// // 取出名为user的session属性
// ZaghUsers user = (ZaghUsers) session.get("userbean");
// // 如果没有登陆,返回重新登陆
// if (user != null||methodName.equalsIgnoreCase("checkUserLogin")) {
// return invocation.invoke();
// } else {
// return "/tpl/html//home/Home/index.jsp";
// }
// }

}
=====================struts.xml==============================
<?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>

<!--*********************struts2 constant************************ -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.custom.i18n.resources" value="messages"/>
<constant name= "struts.multipart.maxSize" value="52428800" />

<package name="default" extends="struts-default" >
<result-types>
<result-type name="direct" class="com.gctx.framework.struts2.dispatcher.DirectResultDispatcher"/>
</result-types>

<!-- 定义一个名为 authority的拦截器 -->
<interceptors>
<!-- 定义权限检查拦截器 -->
<interceptor name="requestAction"
class="com.gctx.web.action.home.UtilInterceptor" />

<!-- 定义一个包含权限检查的拦截器栈 -->
<interceptor-stack name="myDefaultStack">
<!-- 定义拦截器栈包含authority拦截器 需要过滤的方法 index 要加在此 -->
<interceptor-ref name="requestAction">
<param name="excludeMethods">*.index,*.checkUserLogin</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 设置全局默认的拦截器栈-->
<default-interceptor-ref name="myDefaultStack" />
</package>
<package name="all" extends="default">
<result-types>
<result-type name="direct" class="com.gctx.framework.struts2.dispatcher.DirectResultDispatcher"/>
</result-types>
<action name="*" class="com.gctx.web.action.GenericBaseAction" >
<result name="*" type="direct">direct</result>
</action>
</package>



</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值