struts2学笔记十 拦截器

本文详细介绍了Struts2框架中的拦截器实现,包括简单拦截器、方法过滤拦截器及权限控制拦截器的设计与配置。通过示例展示了如何自定义拦截器逻辑,设置拦截器顺序及实现基于用户权限的访问控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7.3简单拦截器
package lee;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.*;

/**
 * @author  yeeku.H.lee kongyeeku@163.com
 * @version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date:
 */
public class SimpleInterceptor extends AbstractInterceptor
{
 private String name;
 public void setName(String name)
 {
  this.name = name;
 }

    public String intercept(ActionInvocation invocation) throws Exception
 {
  LoginAction action = (LoginAction)invocation.getAction();
  System.out.println(name + " 拦截器的动作---------" + "开始执行登陆Action的时间为:" + new Date());
  long start = System.currentTimeMillis();
  String result = invocation.invoke();
  System.out.println(name + " 拦截器的动作---------" + "执行完登陆Action的时间为:" + new Date());
  long end = System.currentTimeMillis();
  System.out.println(name + " 拦截器的动作---------" + "执行完该Action的事件为" + (end - start) + "毫秒");
  return result;
    }
}

struts.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.custom.i18n.resources" value="globalMessages"/>
 <constant name="struts.i18n.encoding" value="GBK"/>

    <package name="lee" extends="struts-default">
  <interceptors>
   <interceptor name="mysimple" class="lee.SimpleInterceptor">
    <param name="name">简单拦截器</param>
   </interceptor>
  </interceptors>

        <action name="login" class="lee.LoginAction">
            <result name="error">/error.jsp</result>
            <result name="success">/welcome.jsp</result>

   <!-- 拦截器一般配置在result元素之后! -->
   <interceptor-ref name="defaultStack"/>
   <interceptor-ref name="mysimple">
    <param name="name">改名后的拦截器</param>
   </interceptor-ref>
        </action>
    </package>
</struts>


7.4深入拦截 器

1方法过滤
package lee;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import java.util.*;

public class MyFilterInterceptor extends MethodFilterInterceptor

{
 private String name;
 public void setName(String name)
 {
  this.name = name;
 }

 public String doIntercept(ActionInvocation invocation)
  throws Exception
 {
  LoginAction action = (LoginAction)invocation.getAction();
  System.out.println(name + " 拦截器的动作---------" + "开始执行登陆Action的时间为:" + new Date());
  long start = System.currentTimeMillis();
  String result = invocation.invoke();
  System.out.println(name + " 拦截器的动作---------" + "执行完登陆Action的时间为:" + new Date());
  long end = System.currentTimeMillis();
  System.out.println(name + " 拦截器的动作---------" + "执行完该Action的事件为" + (end - start) + "毫秒");
  return result;
 }

}

struts.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.custom.i18n.resources" value="globalMessages"/>
 <constant name="struts.i18n.encoding" value="GBK"/>

    <package name="lee" extends="struts-default">
  <interceptors>
   <interceptor name="myfilter" class="lee.MyFilterInterceptor">
    <param name="name">方法过滤拦截器</param>
   </interceptor>

  </interceptors>

        <action name="login" class="lee.LoginAction">
            <result name="error">/error.jsp</result>
            <result name="success">/welcome.jsp</result>

   <!-- 拦截器一般配置在result元素之后! -->
   <interceptor-ref name="defaultStack"/>
   <interceptor-ref name="myfilter">
    <param name="name">改名后的方法过滤拦截器</param>
    <param name="excludeMethods">haha</param>
    <param name="includeMethods">execute</param>
   
   </interceptor-ref>
        </action>
    </package>
</struts>


excludeMethods  过滤掉方法为haha
includeMethods 对execute方法进行拦截


2拦截器的顺序
如果是在拦截方法之前,则配置在前面的拦截器,会先对用户的请求起作用。如果是在拦截方法之后,则配置在后面的拦截器,会先对用户的请求起作用。
例如:
<interceptor-ref name="defaultStack"/>
   <interceptor-ref name="mysimple">
    <param name="name">第一个</param>
   </interceptor-ref>
   <interceptor-ref name="mysimple">
    <param name="name">第二个</param>
   </interceptor-ref>

结果:
第一个 拦截器的动作---------开始执行登陆Action的时间为:Wed Jan 12 10:17:16 CST 2011
第二个 拦截器的动作---------开始执行登陆Action的时间为:Wed Jan 12 10:17:16 CST 2011
进入execute方法执行体..........
第二个 拦截器的动作---------执行完登陆Action的时间为:Wed Jan 12 10:17:24 CST 2011
第二个 拦截器的动作---------执行完该Action的事件为8522毫秒
第一个 拦截器的动作---------执行完登陆Action的时间为:Wed Jan 12 10:17:24 CST 2011
第一个 拦截器的动作---------执行完该Action的事件为8522毫秒


3拦截结果的监听器
public class MyPreResultListener implements PreResultListener
{
 public void beforeResult(ActionInvocation invocation,String resultCode)
 {
  System.out.println("返回的逻辑视图为:" + resultCode);
 }
}

配置中 没有配置,怎么对跳转的结果进行拦截?
execute方法执行之前的拦截...
进入execute方法执行体..........
返回的逻辑视图为:error
execute方法执行之后的拦截......

4 ...*

5拦截器实现权限控制
package lee;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.*;
public class AuthorityInterceptor extends AbstractInterceptor
{
    public String intercept(ActionInvocation invocation) throws Exception
 {
  ActionContext ctx = invocation.getInvocationContext();
  Map session = ctx.getSession();
  String user = (String)session.get("user");
  if (user != null && user.equals("pp") )
  {
   return invocation.invoke();
  }
  ctx.put("tip" , "您还没有登陆,请输入pp,911r登陆系统");
  return Action.LOGIN;
    }
}
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "
http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.custom.i18n.resources" value="globalMessages"/>
 <constant name="struts.i18n.encoding" value="GBK"/>

    <package name="lee" extends="struts-default">
  <interceptors>
   <interceptor name="authority" class="lee.AuthorityInterceptor"/>
  </interceptors>

  <global-results>
            <result name="login">/login.jsp</result>
  </global-results>

        <action name="login" class="lee.LoginAction">
            <result name="error">/error.jsp</result>
            <result name="success">/welcome.jsp</result>
        </action>
        <action name="viewBook">
            <result>/WEB-INF/jsp/viewBook.jsp</result>
   <!-- 拦截器一般配置在result元素之后! -->
   <interceptor-ref name="defaultStack"/>
   <interceptor-ref name="authority"/>
        </action>
    </package>
</struts>

页面中<a href="viewBook.action">查看作者李刚出版的图书</a>
 点击链接时进入了拦截. 如果用户名为pp就可以跳转到 <result>/WEB-INF/jsp/viewBook.jsp</result>
${requestScope.tip}
否则提示
您还没有登陆,请输入pp,911登陆系统

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值