struts2 拦截器拦截action中指定方法



struts2 拦截器拦截action中指定方法

1.继承类MethodFilterInterceptor(此类是类AbstractInterceptor的子类)

import java.util.Map;

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

/*
 *拦截指定方法 
 */
public class MyFilterInterceptor  extends MethodFilterInterceptor{
 private static final long serialVersionUID = 1L;
 private String name;
    public void setName(String name)
   {
        this.name = name;
    }
 @Override
 protected String doIntercept(ActionInvocation invocation) throws Exception {
  //取得请求相关的ActionContext实例 
  ActionContext ctx = invocation.getInvocationContext(); 
  Map session = ctx.getSession(); 
  //取出名为user的Session属性 
  String user = (String)session.get("user"); 
  //如果没有登陆,或者登陆所用的用户名不是scott,都返回重新登陆 
  if (user != null && user.equals("scott") ) 
  { 
  return invocation.invoke(); 
  } 
  //没有登陆,将服务器提示设置成一个HttpServletRequest属性 
  ctx.put("tip" , "您还没有登陆,请输入scott,tiger登陆系统"); 
  //直接返回login的逻辑视图 
  return Action.LOGIN; 
 }

}

2.struts.xml配置

<package name="site" extends="struts-default" namespace="/site">
 <interceptors> 
 <!-- 定义了一个名为authority的拦截器 --> 
  <interceptor name="authority"/> <!--上面自定义的拦截器类-->
  <interceptor-stack name="myDefault">
   <interceptor-ref name="authority"> <!-- 引用拦截器  -->
    <param name="includeMethods">getALL,getPart,listUser</param> <!-- 设置需要拦截的方法,多个以逗号隔开 -->
   </interceptor-ref>
   <interceptor-ref name="defaultStack"></interceptor-ref>
  </interceptor-stack>
 </interceptors>
 <default-interceptor-ref name="myDefault"></default-interceptor-ref>

  <!-- 全局 -->
 <global-results> 
  <!-- 当返回login视图名时,转入/login.jsp页面 --> 
  <result name="login">/login.jsp</result> 
 </global-results>
<action name="site">

  <!--省略跳转-->

</action>

</package>


转自:http://enetq.blog.51cto.com/479739/542619

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

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

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

查看API文档 可以看到这个类为:

 

  1. /*
  2.  * Copyright 2002-2006,2009 The Apache Software Foundation.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the ”License”);
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an ”AS IS” BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package com.opensymphony.xwork2.interceptor;
  17. import com.opensymphony.xwork2.ActionInvocation;
  18. import com.opensymphony.xwork2.util.TextParseUtil;
  19. import com.opensymphony.xwork2.util.logging.Logger;
  20. import com.opensymphony.xwork2.util.logging.LoggerFactory;
  21. import java.util.Collections;
  22. import java.util.Set;
  23. /**
  24.  * <!– START SNIPPET: javadoc –>
  25.  *
  26.  * MethodFilterInterceptor is an abstract <code>Interceptor</code> used as
  27.  * a base class for interceptors that will filter execution based on method
  28.  * names according to specified included/excluded method lists.
  29.  *
  30.  * <p/>
  31.  *
  32.  * Settable parameters are as follows:
  33.  *
  34.  * <ul>
  35.  *      <li>excludeMethods - method names to be excluded from interceptor processing</li>
  36.  *      <li>includeMethods - method names to be included in interceptor processing</li>
  37.  * </ul>
  38.  *
  39.  * <p/>
  40.  *
  41.  * <b>NOTE:</b> If method name are available in both includeMethods and
  42.  * excludeMethods, it will be considered as an included method:
  43.  * includeMethods takes precedence over excludeMethods.
  44.  *
  45.  * <p/>
  46.  *
  47.  * Interceptors that extends this capability include:
  48.  *
  49.  * <ul>
  50.  *    <li>TokenInterceptor</li>
  51.  *    <li>TokenSessionStoreInterceptor</li>
  52.  *    <li>DefaultWorkflowInterceptor</li>
  53.  *    <li>ValidationInterceptor</li>
  54.  * </ul>
  55.  *
  56.  * <!– END SNIPPET: javadoc –>
  57.  *
  58.  * @author <a href=’mailto:the_mindstorm[at]evolva[dot]ro’>Alexandru Popescu</a>
  59.  * @author Rainer Hermanns
  60.  *
  61.  * @see org.apache.struts2.interceptor.TokenInterceptor
  62.  * @see org.apache.struts2.interceptor.TokenSessionStoreInterceptor
  63.  * @see com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor
  64.  * @see com.opensymphony.xwork2.validator.ValidationInterceptor
  65.  *
  66.  * @version $Date: 2009-12-27 19:18:29 +0100 (Sun, 27 Dec 2009) $ $Id: MethodFilterInterceptor.java 894090 2009-12-27 18:18:29Z martinc $
  67.  */
  68. public abstract class MethodFilterInterceptor extends AbstractInterceptor {
  69.     protected transient Logger log = LoggerFactory.getLogger(getClass());
  70.     protected Set<String> excludeMethods = Collections.emptySet();
  71.     protected Set<String> includeMethods = Collections.emptySet();
  72.     public void setExcludeMethods(String excludeMethods) {
  73.         this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);
  74.     }
  75.     public Set<String> getExcludeMethodsSet() {
  76.         return excludeMethods;
  77.     }
  78.     public void setIncludeMethods(String includeMethods) {
  79.         this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods);
  80.     }
  81.     public Set<String> getIncludeMethodsSet() {
  82.         return includeMethods;
  83.     }
  84.     @Override
  85.     public String intercept(ActionInvocation invocation) throws Exception {
  86.         if (applyInterceptor(invocation)) {
  87.             return doIntercept(invocation);
  88.         }
  89.         return invocation.invoke();
  90.     }
  91.     protected boolean applyInterceptor(ActionInvocation invocation) {
  92.         String method = invocation.getProxy().getMethod();
  93.         // ValidationInterceptor
  94.         boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method);
  95.         if (log.isDebugEnabled()) {
  96.             if (!applyMethod) {
  97.                 log.debug(“Skipping Interceptor… Method [" + method + "] found in exclude list.”);
  98.             }
  99.         }
  100.         return applyMethod;
  101.     }
  102.     /**
  103.      * Subclasses must override to implement the interceptor logic.
  104.      *
  105.      * @param invocation the action invocation
  106.      * @return the result of invocation
  107.      * @throws Exception
  108.      */
  109.     protected abstract String doIntercept(ActionInvocation invocation) throws Exception;
  110. }

是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():获得拦截器的白名单

 

一般开发者只需要重写doIntercept方法即可!下面给出一个实例:

一。实现方法过滤的拦截器实现类:FilterInterceptor.java继承自MethodFilterInterceptor类

 

  1. package com.yaxing.interceptor;
  2. import java.util.Date;
  3. import com.opensymphony.xwork2.ActionInvocation;
  4. import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
  5. public class FilterInterceptor extends MethodFilterInterceptor {
  6.     private String name;
  7.     public String getName() {
  8.         return name;
  9.     }
  10.     public void setName(String name) {
  11.         this.name = name;
  12.     }
  13.     @Override
  14.     protected String doIntercept(ActionInvocation invocation) throws Exception {
  15.         // TODO Auto-generated method stub
  16.         FilterAction fa= (FilterAction)invocation.getAction();
  17.         System.out.println(name+”拦截器在Action执行前拦截”+new Date());
  18.         String result=invocation.invoke();
  19.         System.out.println(name+”拦截器在Action执行后拦截”+new Date());
  20.         return result;
  21.     }
  22. }

在该类中name属性用来标识拦截器的名称,方便控制台的输出

二。Action:业务控制器FilterAction.java

 

  1. package com.yaxing.interceptor;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class FilterAction extends ActionSupport {
  4.     private String msg;
  5.     public String getMsg() {
  6.         return msg;
  7.     }
  8.     public void setMsg(String msg) {
  9.         this.msg = msg;
  10.     }
  11.     public String method1() throws Exception {
  12.         System.out.println(“Action执行方法:method1()”);
  13.         return SUCCESS;
  14.     }
  15.     public String method2() throws Exception {
  16.         System.out.println(“Action执行方法:method2()”);
  17.         return SUCCESS;
  18.     }
  19.     public String method3() throws Exception {
  20.         System.out.println(“Action执行方法:method3()”);
  21.         return SUCCESS;
  22.     }
  23. }

三。配置文件struts.xml

  1. <?xml version=”1.0″ encoding=”UTF-8″ ?>
  2. <!DOCTYPE struts PUBLIC ”-//Apache Software Foundation//DTD Struts Configuration 2.1//EN” ”http://struts.apache.org/dtds/struts-2.1.dtd”>
  3. <struts>
  4. <package name=”filterexample” extends=”struts-default” namespace=”/ch5″>
  5.     <interceptors>
  6. <!–定义拦截器–>
  7.         <interceptor name=”Myinterceptor” class=”com.yaxing.interceptor.Myinterceptor”></interceptor>
  8.         <interceptor name=”SimpleInterceptor” class=”com.yaxing.interceptor.SimpleInterceptor”></interceptor>
  9.         <interceptor name=”FilterInterceptor” class=”com.yaxing.interceptor.FilterInterceptor”></interceptor>
  10.     </interceptors>
  11.     <action  name=”Reg” class=”com.yaxing.interceptor.Reg” method=”execute”>
  12.            <result name=”success”>/Success.jsp</result>
  13.            <result name=”input”>/Reg.jsp</result>
  14.            <interceptor-ref name=”defaultStack”></interceptor-ref>
  15.            <interceptor-ref name=”SimpleInterceptor”></interceptor-ref>
  16.     </action>
  17.     <action name=”FilterAction” class=”com.yaxing.interceptor.FilterAction”>
  18.             <result name=”success”>/MethodFilter.jsp</result>
  19. <!–使用拦截器–>
  20.             <interceptor-ref name=”defaultStack”></interceptor-ref>
  21.             <interceptor-ref name=”FilterInterceptor”>
  22. <!–拦截器黑白名单–>
  23.                  <param name=”includeMethods”>method1</param>
  24.                  <param name=”excludeMethods”>method2</param>
  25. <!–指定参数name的值–>
  26. <param name=”name”>FilterMethod</param>
  27.             </interceptor-ref>
  28.     </action>
  29. </package>
  30. </struts>

四。jsp视图 MethodFilter.jsp

 

  1. <%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
  2. <%@ taglib prefix=”s” uri=”/struts-tags” %>
  3. <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN”>
  4. <html>
  5.   <head>
  6.     <title>My JSP ’MethodFilter.jsp’ starting page</title>
  7.     <meta http-equiv=”pragma” content=”no-cache”>
  8.     <meta http-equiv=”cache-control” content=”no-cache”>
  9.     <meta http-equiv=”expires” content=”0″>
  10.     <meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
  11.     <meta http-equiv=”description” content=”This is my page”>
  12.     <!–
  13.     <link rel=”stylesheet” type=”text/css” href=”styles.css”>
  14.     –>
  15.   </head>
  16.   <body>
  17.     <s:form id=”id” action=”FilterAction!method1.action”>
  18.        <s:textfield name=”msg” label=”请输入信息:”/>
  19.        <s:submit value=”提交”/>
  20.     </s:form>
  21.   </body>
  22. </html>

五:运行结果:

 

 

可以看出method1方法拦截了。而method2方法是放到了黑名单中。

②将JSP视图中action 换成FilterAction!method2.action 输出信息

 

因为指定method2为黑名单,不会拦截,因此是没有拦截信息的。

③将JSP视图中action 换成FilterAction!method3.action 输出信息

可以看到,虽然没有在黑名单中指定method3,因为配置文件显示指定了白名单,所以拦截器只拦截白名单中指定的方法!

http://www.cnblogs.com/ribavnu/archive/2013/03/15/2960891.html

Struts2拦截器的方法过滤

struts2框架提供了MethodInterceptor类, 自定义的拦截器只要集成该类,就可以使用拦截器的方法过滤功能,来拦截Action中的特定方法。

(可以查看MethodInterceptor文档,在此声明,我没查。com.opensymphony.xwork2.interceptor.AbstractInterceptor拦截器的子类,实现类Interceptor和Serializable接口。)
两个重要的参数:
1,excludeMethods:该参数指定拦截器拒绝拦截的方法列表,如果有多个方法值,中间用逗号法恩凯,例如“method1,method2”。如果指定了该参数,那么该拦截器不会拦截指定的Action方法,类似于所谓的黑名单。
2,includeMethods:该参数指定拦截器需要拦截的方法列表,如果有多个方法值,中间使用逗号分开。如果指定了该参数,则指定的Action在执行前会被拦截器所拦截,即所谓的白名单。
注意:如果黑名单和白名单指定了同一个Action方法那么该方法还是会别拦截。
MethodInterceptor类的主要方法:
protected abstract String doIntercept(ActionInvocation invocation):继承该类的子类必须重写该方法,并实现拦截器逻辑。(如果没继承MethodInterceptor类强制重写的是intercept()方法而不是doIntercept()方法);
String intercept(ActionInvocation invocation): 继承子com.opensymphony.xwork2.interceptor.AbstractInterceptor类,该方法不需要强制重写。
void setExcludeMethods(String includeMethods):设置拦截器的白名单,该方法参数为一个字符串,即对应的Action方法名称。使用该方法设置后,拦截器会拦截该方法。
set getExcludeMethodsSet():获得拦截器的黑名单。
Set getIncludeMethodsSet():获得拦截器的白名单。
如果使用MethodFilterInterceptor类的子类来是想拦截器的方法过滤,不需要同前面那样来重写intercept(ActionInvocation invocation)方法,这里只需重写doIntercept(ActionInvocation  invocation)方法即可。
说明:方法过滤拦截器和普通拦截器用法没什么区别,只是继承了com.opensymphony.xwork2.interceptor.AbstractInterceptor类的子类MethoidFilterInterceptor,并重写doIntercept(ActionInvocation invocation)方法;
实现方法过滤拦截的配置文件示例:

[html] view plain copy

  1. <action name=”FilterAction” class=”FilterActionClass”>
  2. <result name=”success”>/success.jsp</result>
  3. <result name=”input”>/error.jsp</result>
  4. <interceptor-ref name=”defaultStack”></interceptor>
  5. <interceptor-ref name=”FilterInterceptor”>
  6. <!–使用方法过滤,设置白名单或黑名单–>
  7. <param name=”excludeMethods”>method1</param>      <!–不拦截Action中的method1–>
  8. <param name=”includeMethods”>method2</param>       <!–拦截Action中的method2–>
  9. </action>

struts2框架提供了MethodInterceptor类, 自定义的拦截器只要集成该类,就可以使用拦截器的方法过滤功能,来拦截Action中的特定方法。

(可以查看MethodInterceptor文档,在此声明,我没查。com.opensymphony.xwork2.interceptor.AbstractInterceptor拦截器的子类,实现类Interceptor和Serializable接口。)
两个重要的参数:
1,excludeMethods:该参数指定拦截器拒绝拦截的方法列表,如果有多个方法值,中间用逗号法恩凯,例如“method1,method2”。如果指定了该参数,那么该拦截器不会拦截指定的Action方法,类似于所谓的黑名单。
2,includeMethods:该参数指定拦截器需要拦截的方法列表,如果有多个方法值,中间使用逗号分开。如果指定了该参数,则指定的Action在执行前会被拦截器所拦截,即所谓的白名单。
注意:如果黑名单和白名单指定了同一个Action方法那么该方法还是会别拦截。
MethodInterceptor类的主要方法:
protected abstract String doIntercept(ActionInvocation invocation):继承该类的子类必须重写该方法,并实现拦截器逻辑。(如果没继承MethodInterceptor类强制重写的是intercept()方法而不是doIntercept()方法);
String intercept(ActionInvocation invocation): 继承子com.opensymphony.xwork2.interceptor.AbstractInterceptor类,该方法不需要强制重写。
void setExcludeMethods(String includeMethods):设置拦截器的白名单,该方法参数为一个字符串,即对应的Action方法名称。使用该方法设置后,拦截器会拦截该方法。
set getExcludeMethodsSet():获得拦截器的黑名单。
Set getIncludeMethodsSet():获得拦截器的白名单。
如果使用MethodFilterInterceptor类的子类来是想拦截器的方法过滤,不需要同前面那样来重写intercept(ActionInvocation invocation)方法,这里只需重写doIntercept(ActionInvocation invocation)方法即可。
说明:方法过滤拦截器和普通拦截器用法没什么区别,只是继承了com.opensymphony.xwork2.interceptor.AbstractInterceptor类的子类MethoidFilterInterceptor,并重写doIntercept(ActionInvocation invocation)方法;

 

 

关于struts2的自定义拦截器和struts2的详细流程 – Java

http://www.94cto.com/index/Article/content/id/63218.html

1.其实我们大家平常都会用struts2用的很多,但是有的时候我们并不是真正的了解struts2的运行机制,下面给大家分享一下struts2的运行流程。MVC框架


\

解释如下:

1. 所有请求被Struts2核心控制器StrutsPreparaedAndExecuteFilter拦截

2.根据ActionMapper提供的信息决定如何进行下一步

3.ActionMapper主要依赖Struts2的配置文件struts.xml

4.接下来为每个Action创建Action代理类ActionProxy

5.执行ActionProxy的execute()方法

6.在执行execute()方式时会逐个执行Struts2中的拦截器

7.执行完成拦截器后才会真正执行目标Action

8.目标Action需要返会转向的视图名称

9.ActionProxy取得视图名称对象Result完成转向

10.生成resposne对象完成本次请求

2.在struts2中默认有18个拦截器,他们帮助完成很多的工作,比如像处理异常啊,国际化,文件上传等等18个拦截器如下,如果没有拦截器我们都没法为属性设值,所以在struts2的xml文件中,我们必须需要extends=“struts-defalut”。

 

<span style="font-size:18px;"><interceptor-stack name="defaultStack">
                <interceptor-ref name="exception"/>
                <interceptor-ref name="alias"/>
                <interceptor-ref name="servletConfig"/>
                <interceptor-ref name="i18n"/>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="chain"/>
                <interceptor-ref name="debugging"/>
                <interceptor-ref name="scopedModelDriven"/>
                <interceptor-ref name="modelDriven"/>
                <interceptor-ref name="fileUpload"/>
                <interceptor-ref name="checkbox"/>
                <interceptor-ref name="multiselect"/>
                <interceptor-ref name="staticParams"/>
                <interceptor-ref name="actionMappingParams"/>
                <interceptor-ref name="params">
                  <param name="excludeParams">dojo..*,^struts..*</param>
                </interceptor-ref>
                <interceptor-ref name="conversionError"/>
                <interceptor-ref name="validation">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
                <interceptor-ref name="workflow">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
            </interceptor-stack>
</span>

3.如果我们想扩展框架的功能,那么我们就需要自己写拦截器,那么如何来实现拦截器呢?

 

① 首先在创建计算action执行时间拦截器时,两种方法,继承父类:AbstractInterceptor,实现接口:Interceptor。重写里边的方法:intercept()方法。代码如下:

第一种方式:

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

public class MyLogInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("开始记录日志");
		//继续执行(调用后面的拦截器(取决于actionInvocation放置拦截器的迭代器中是否存在拦截器)
		//如果存在拦截器就执行拦截器,否则执行Action)
		String resultCode = invocation.invoke();
		System.out.println("结束记录日志");
		return resultCode;
	}

}

第二种方式

 

 

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

public class MySecurityInterceptor implements Interceptor {

	public void destroy() {
	}

	public void init() {
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("开始检查安全性");
		//继续调用
		String resultCode = invocation.invoke();
		System.out.println("结束检查安全性");
		return resultCode;
	}

}

 

②然后struts.xml中需要声明我们拦截器,如果直接声明,那么我们默认的18个拦截器将不起作用,如何配置及时每个action都能起到作用,又是所有的拦截器都执行呢?需要利用到拦截器栈来对框架默认的拦截器栈进行覆盖:(代码如下)

 

<?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>
	<!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	<!-- 会提供更加友好的提示信息 -->
	<constant name="struts.devMode" value="true"/>
	<!-- 需要继承struts-default包,这样就拥有的最基本的功能 -->
	<package name="struts2" extends="struts-default">
		<interceptors>
			<!-- 定义记录日志拦截器 -->
			<interceptor name="myLogInterceptor" class="com.bjpowernode.struts2.MyLogInterceptor"/>
			<!-- 定义检查安全性拦截器 -->
			<interceptor name="mySecurityInterceptor" class="com.bjpowernode.struts2.MySecurityInterceptor"/>

			<interceptor-stack name="myInterceptorStack">
				 <interceptor-ref name="defaultStack"/>
				 <interceptor-ref name="myLogInterceptor"/>
				 <interceptor-ref name="mySecurityInterceptor"/>
			</interceptor-stack>
		</interceptors>
		<!-- 定义为缺省拦截器,所有的Action都会得到使用 -->
		<default-interceptor-ref name="myInterceptorStack"/>		
		<action name="login" class="com.bjpowernode.struts2.LoginAction">
			<!-- 
				使用日志记录拦截器 
				如果自定了拦截器,必须显示引用Struts2默认的拦截器
			<interceptor-ref name="myLogInterceptor"/>
			-->
		    <!-- 引用Struts2默认的拦截器 -->
		    <!-- 
		    <interceptor-ref name="defaultStack"/>
		     -->
			<!-- 使用日志记录拦截器  -->
			<!-- 
			<interceptor-ref name="myLogInterceptor"/>
			 -->
			<!-- 使用检查安全性拦截器 -->
			<!-- 
			<interceptor-ref name="mySecurityInterceptor"/>
			 -->
			 <!-- 引入自定义的拦截器栈 -->
			<!-- 
			<interceptor-ref name="myInterceptorStack"/>
			 -->
			<result>/login_success.jsp</result> 
			<result name="error">/login_error.jsp</result>
		</action>
	</package>
</struts>

4.总结一下

 

Struts2的拦截器

1、Struts2的拦截器只能拦截Action,拦截器是AOP的一种思路,可以使我们的系统架构
更松散(耦合度低),可以插拔,容易互换,代码不改变的情况下很容易满足客户需求
其实体现了OCP

2、如何实现拦截器?(整个拦截器体现了责任链模式,Filter也体现了责任链模式)
* 继承AbstractInterceptor(体现了缺省适配器模式)
* 实现Interceptor

3、如果自定了拦截器,缺省拦截器会失效,必须显示引用Struts2默认的拦截器

4、拦截器栈,多个拦截器的和

5、定义缺省拦截器<default-interceptor-ref>,所有的Action都会使用

6、拦截器的执行原理,在ActionInvocation中有一个成员变量Iterator,这个Iterator中保存了所有的
拦截器,每次都会取得Iterator进行next,如果找到了拦截器就会执行,否则就执行Action,都执行完了
拦截器出栈(其实出栈就是拦截器的intercept方法弹出栈)

 

 

Struts2拦截器设置例外情况

权限拦截器如下:

 

[java] view plain copy

在CODE上查看代码片派生到我的代码片

  1. /**
  2.  * 权限拦截器
  3.  * 拦截非登录用户
  4.  * @author Administrator
  5.  *
  6.  */
  7. public class AuthorInterceptor extends AbstractInterceptor{
  8.     @Override
  9.     public String intercept(ActionInvocation arg0) throws Exception {
  10.         //如果是登录请求则直接放行
  11.         Object o = arg0.getAction();
  12.         System.out.println(o.getClass());
  13.         if(o instanceof LoginAction){
  14.             return arg0.invoke();
  15.         }
  16.         //拦截非登录用户
  17.         //获取Session
  18.         Map session = arg0.getInvocationContext().getSession();
  19.         //判断用户是否已登录
  20.         Users u = (Users)session.get(“user”);
  21.         if(u==null){
  22.             //未登录用户,将被拦截到登录页面
  23.             System.out.println(“非法请求已被拦截,系统将跳转到登录页面!!!!!!!”);
  24.             return ”login”;
  25.         }else{
  26.             //放行
  27.             return arg0.invoke();
  28.         }
  29.     }
  30. }

配置如下:

 

 

[html] view plain copy

在CODE上查看代码片派生到我的代码片

  1. <struts>
  2.     <!– 配置编码(防止中文乱码) –>
  3.     <constant name=”struts.i18n.encoding” value=”utf-8″></constant>
  4.     <package name=”default” extends=”struts-default” namespace=”/”>
  5.         <!– 拦截器定义 –>
  6.         <interceptors>
  7.             <!–注册自定义的拦截器 –>
  8.             <interceptor name=”auth” class=”com.hr.interceptor.AuthorInterceptor”></interceptor>
  9.             <!– 自定义拦截器栈 –>
  10.             <interceptor-stack name=”myStack”>
  11.                 <!– 自定义拦截器栈的第一个拦截器一定要配置成系统默认拦截器 –>
  12.                 <interceptor-ref name=”defaultStack”></interceptor-ref>
  13.                 <!– 将自定义的拦截器加入到自定义的拦截器栈 –>
  14.                 <interceptor-ref name=”auth”></interceptor-ref>
  15.             </interceptor-stack>
  16.         </interceptors>
  17.         <!– 将自定义拦截器栈设置为默认拦截器 –>
  18.         <default-interceptor-ref name=”myStack”/>
  19.         <!– 配置默认Action(当其他Action不能匹配的时候自动匹配此Action –>
  20.         <default-action-ref name=”defaultAction”/>
  21.         <!– 定义全局视图 –>
  22.         <global-results>
  23.             <result name=”error”>error.jsp</result>
  24.             <result name=”404″>/404.jsp</result>
  25.             <result name=”login”>/login.jsp</result>
  26.         </global-results>
  27.         <!– 定义默认Action –>
  28.         <action name=”defaultAction”>
  29.             <!– name属性默认值为 success –>
  30.             <result>/404.jsp</result>
  31.         </action>
  32.         <action name=”login” class=”com.action.LoginAction”>
  33.             <result name=”index”>index.jsp</result>
  34.         </action>
  35.         <!– 用户处理Action –>
  36.         <action name=”user” class=”com.action.UserAction”>
  37.             <!– 动态结果 –>
  38.             <result type=”redirectAction”>${nextPos}</result>
  39.             <result name=”index”>/index.jsp</result>
  40.             <result name=”ok”>/ok.jsp</result>
  41.             <!– redirectAction类型用于Action之间的转发 –>
  42.             <result name=”m” type=”redirectAction”>/manage!mt.action</result>
  43.             <result name=”update_user”>/user_update.jsp</result>
  44.         </action>
  45.         <!– 管理Action –>
  46.         <action name=”manage” class=”com.action.ManageAction”>
  47.             <result>/ok.jsp</result>
  48.         </action>
  49.     </package>
  50. </struts>

 

参考:http://tanglei528.blog.163.com/blog/static/4335339920109993937618/

http://blog.csdn.net/javaalpha/article/details/9399287

  1. http://blog.knowsky.com/189061.htm
  2. http://www.cnblogs.com/ribavnu/archive/2013/03/15/2960891.html
  3. http://www.94cto.com/index/Article/content/id/63218.html
  4. http://bbs.csdn.net/topics/290048056
  5. http://blog.csdn.net/wingbin/article/details/24018639