Struts2的拦截器过滤方法

在Action中使用拦截器,默认情况下回拦截Action中所有的方法,但是在某些情况下,可能只需要拦截Action中的一个或多个方法,有时候也希望不拦截某个方法,这个在Struts2中是怎么实现的呢?

 

拦截器方法过滤:让拦截器有选择的拦截Action中的某个方法!

 

Struts2中提供了一个MethodFilterInterceptor类,开发者自定义的拦截器只需要继承该类就可以使用这个方法过滤的功能,来拦截Action中特定的方法!

 

MethodFilterInterceptor类是AbstractInterceptor拦截器的子类,实现了Interceptor和Serializable接口

 

MethodFilerInterceptor实现方法过滤中用到的两个参数

execludeMethods:该参数指定拦截器拒绝拦截的方法列表,多个方法用“,”隔开,指定了这个参数,拦截器不会拦截指定列表中的方法,就是所谓的黑名单
includeMethods:该参数指定拦截器需要拦截的方法列表,如果指定了参数,则指定的Action在执行前会被拦截,即白名单。

 

主要方法:

①protected abstract String doIntercept(ActionInvocation invocation) throws Exception;  必须重写此方法,实现拦截。

②String interceptor(ActionInvocation invocation):继承自AbstractInterceptor类,方法不需要强制重写

③void setExcludeMethods(String excludeMethods):设置拦截器黑名单,参数为Action一方法名。拦截器不拦截该方法

④void setIncludeMethods(String includeMethods):设置拦截器白名单,参数为Action一方法名。拦截器会拦截该方法

⑤Set<String> getExcludeMethodsSet():获得拦截器的黑名单

⑥Set<String> getIncludeMethodsSet():获得拦截器的白名单

 

具体演示代码如下:

Action类:

package com.accp.struts2.interceptor;

import com.opensymphony.xwork2.ActionSupport;

public class DemoAction02 extends ActionSupport{

 private String name;
 private String password;
 
 public String getName() {
  return name;
 }

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

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 @Override
 public String execute() throws Exception {
  System.out.println("this is execute method");
  System.out.println("name = " + this.getName() + "tpassword = " + this.getPassword());
  return SUCCESS;
 }

 public String hello(){
  System.out.println("this is hello method");
  return "success";
 }

 public String world(){
  System.out.println("this is world method");
  return "success";
 }

}

 

拦截器:

package com.accp.struts2.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class Interceptor02 extends MethodFilterInterceptor {

 @Override
 protected String doIntercept(ActionInvocation arg0) throws Exception {
  DemoAction02 da = (DemoAction02)arg0.getAction();
  System.out.println(da.getClass().getName());

  Map<String,Object> session = arg0.getInvocationContext().getSession();
  session.put("hello", "world");
  System.out.println("interceptor02连接器之前------------");

  //执行该拦截器的后一个拦截器,或者直接指定Action的execute()方法 
  String retVal = arg0.invoke();
  System.out.println("interceptor02连接器之后------------");
  return retVal;
 }
}

配置文件:

<?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="default_intercptor02" namespace="/" extends="struts-default">
     <interceptors>
      <interceptor name="demo02Interceptor" class="com.accp.struts2.interceptor.Interceptor02"></interceptor>
     </interceptors>
     <action name="interceptor2" class="com.accp.struts2.interceptor.DemoAction02">
            <result name="success">chapter7/demo02_result.jsp</result>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="demo02Interceptor">
             <param name="name">改名后的方法过滤拦截器</param>
             <param name="excludeMethods">world</param>   //不被拦截的方法名
             <param name="includeMethods">hello</param>   //被拦截的方法名
            </interceptor-ref>
        </action>
    </package>
</struts>

访问页面:

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<html>
  <head>
    <title>My JSP 'demo01.jsp' starting page</title>
  </head>

  <body>
    <form action="../interceptor2!hello.action" method="post">
     name:<input type="text" name="name"><br/>
     password:<input type="password" name="password"><br/>
     <input type="submit" value="提交"/>
    </form>
  </body>
</html>

 

结果:

第一种:

<form action="../interceptor2!hello.action" method="post">

 

com.accp.struts2.interceptor.DemoAction02
interceptor02连接器之前------------
this is hello method
interceptor02连接器之后------------

 

第二种:

<form action="../interceptor2!world.action" method="post">

com.accp.struts2.interceptor.DemoAction02
this is world method

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值