<?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>
<package name="UserAction" extends="struts-default" namespace="/">
<!-- 【拦截器配置】 -->
<interceptors>
<interceptor name="loginCheck" class="com.hyxd.Interceptor.UserCheckInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="loginCheck"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 【执行拦截器:第一种写法: 当前包下所有的acntion都执行myStack栈】
<default-interceptor-ref name="myStack"></default-interceptor-ref>
-->
<!-- 全局定义 -->
<global-results>
<result name="error">/error.jsp</result>
<result name="input">/login.jsp</result>
</global-results>
<!-- 第一个action -->
<action name="user_*" class="com.hyxd.action.UserAction" method="{1}">
<!-- redirectAction 不会保留request对象 -->
<result type="redirectAction" name="{1}">emp_list</result>
</action>
<!-- 第二个action 接收redirectAction(重定向到新的Action)value=emp_list-->
<action name="emp_*" class="com.hyxd.action.EmployeeAction" method="{1}">
<!--第二种写法: 只是在这一个Action中执行myStack栈
<interceptor-ref name="defaultStackt"></interceptor-ref>
<interceptor-ref name="loginCheck"></interceptor-ref>-->
<!-- 第三种写法:执行用户栈(与第二种写法一样, 只在当前aciton中执行自定义栈) -->
<interceptor-ref name="myStack"></interceptor-ref>
<result name="{1}">/WEB-INF/list.jsp</result>
</action>
</package>
</struts>
自定义拦截器
package com.hyxd.Interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class UserCheckInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//获取ActionContext对象
ActionContext ac = invocation.getInvocationContext();
// 获取action的代理对象
ActionProxy proxy = invocation.getProxy();
//得到代理对象正在运行的方法名
String methodName = proxy.getMethod();
if(!"login".endsWith(methodName)){
//从session中获取用户名
Object obj = ac.getSession().get("userInfo");
if(obj==null){
return "input";
}else{
return invocation.invoke();
}
}else{
return invocation.invoke();
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-4189.html