拦截器的配置

<struts>
  <constant name="struts.action.extension" value="action,,jspx,"/>
  <include file="struts-product.xml"/>
  <include file="struts-user.xml"/>
  <package name="basePackage" extends="struts-default">
  <interceptors>
  <interceptor name="myTimer" class="com.xixingit.interceptor.TimerInterceptor"></interceptor>
  <interceptor name="checkUser" class="com.xixingit.interceptor.CheckUserInterceptor">
  </interceptor>
<interceptor-stack  name="myInterceptor">
<interceptor-ref  name="myTimer"></interceptor-ref>
<interceptor-ref  name="checkUser"></interceptor-ref>
<interceptor-ref  name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 下面这 一行主要是必须要写的,不写的情况下会导致拦截器无法使用-->
<default-interceptor-ref name="myInterceptor"/>
<global-results>
  <result name = "loginerror" type="redirectAction">
  <param name="namespace">/user</param>
  <param name="actionName">loginForm</param>
  <param name="code">001</param>
  </result>
  </global-results>
  </package>

  </struts>





<struts>
  <package name="userPackage" extends = "basePackage" namespace="/user">
  <action name="loginForm" class = "com.xixingit.action.UserAction" method = "loginForm">
  <result>
  /WEB-INF/views/user/login.jsp
  </result>
  </action>
  <action name="tologin" class = "com.xixingit.action.UserAction" method = "login">
  <!-- <interceptor-ref  name="myInterceptor"></interceptor-ref> -->
  <result type = "redirectAction">
  <param name="namespace">/product</param>
  <param name="actionName">index</param>
  </result>
  <result name = "error">
  /WEB-INF/views/user/login.jsp
  </result>
  <result name="input">
  /WEB-INF/views/user/login.jsp
  </result>
  </action>
  </package>
  </struts>


重写的拦截器的类用户验证

public class CheckUserInterceptor extends AbstractInterceptor{
public CheckUserInterceptor(){
System.out.println("create.....");
}
private static final long serialVersionUID = 1L;
private String userName;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext actionContext = invocation.getInvocationContext();
ActionProxy ap = invocation.getProxy();

String namespace = ap.getNamespace();
String actionName = ap.getActionName();

if("/user".equals(namespace)&&("loginForm".equals(actionName)||"tologin".equals(actionName))){
return invocation.invoke();
}else{
Map<String,Object> session = actionContext.getSession();
User currUser = (User) session.get("currUser");
if(null != currUser){
return invocation.invoke();
}else{
return "loginerror";
}
}
}


重写的获取链接运行完的类

public class TimerInterceptor extends AbstractInterceptor{
private static final long serialVersionUID = 1L;
public String intercept(ActionInvocation invocation) throws Exception {
//获取当前的时间,类继承AbstractInterceptor类,其中被继承的类实现了相对应的接口
long startTime = System.currentTimeMillis();
System.out.println("interceptor....");
//获取相应的绝对路径
Object obj = invocation.getAction();
System.out.println("obj:" + obj.getClass().getName());
//这个类中的方法可以进行获取到命名空间,方法和actionName
ActionProxy ap = invocation.getProxy();
System.out.println("action:" + ap.getAction());
System.out.println("actionName:" + ap.getActionName());
System.out.println("method:" + ap.getMethod());
System.out.println("namespace:" + ap.getNamespace());
//result是返回一个Success值,用于拦截器的放行
String result = invocation.invoke();
System.out.println("result:" + result);
long endTime = System.currentTimeMillis();
System.out.println( obj.getClass().getName() + "执行" + ap.getMethod()+ "消耗:"+ (endTime - startTime) + "ms");
return result;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值