---
title: "Struts2拦截器配置"
author: "netxdiy"
date: "2011-03-21T11:11:06Z"
tags: ["Apache", "JSP", "Struts", "XML", "框架"]
categories: ["企业架构"]
slug: "struts2"
draft: false
lastmod: "2011-03-21T16:14:34Z"
id: 969068
url: "https://netxdiy.iteye.com/blog/969068"
---

Struts2框架的大部分功能都是通过拦截器来完成的。默认情况下所以的package都会继承默认的struts-default包，这样就可以使用Struts的大量默认拦截器。 /login.jsp public class LoginIntercept extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub Object username = invocation.getInvocationContext().getSession().get( "username"); Object password = invocation.getInvocationContext().getSession().get( "password"); System.out.println(username+"intercept"+password); if (username == null || password == null) { return "loginFail";//失败调回login.jsp页面 } else { String result = invocation.invoke();//通过 System.out.println(result); return result; } } } web.xml struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* /////////////////////////// 1、自定义全局拦截器 在Struts.xml里增加 为其他包引用的方便可以把多个拦截器放在一起，组成拦截器栈： 2、在action中使用拦截器 3、配置默认的拦截器（每个包只能定义一个默认拦截器） 当配置一个包后，可以为其指定特定的拦截器。这样如果该包中的ACTION没有配置拦截器，那么默认的拦截器将起作用 4、实现拦截器需继承AbstractInterceptor类 5、在默认情况下，如何某个Action定义了拦截器，这个拦截器会拦截Action的所以方法，如果不想拦截所以方法，可以使用Struts2的方法过滤特性。 拦截器别名1,拦截器别名2 &lt; ————屏蔽的拦截 拦截器别名2 &lt;————开启拦截 6、Struts2拦截器的执行顺序 并不是先配置的先拦截，在execute方法执行前的动作按顺序拦截，在execute方法执行之后的动作逆序执行，也就是说配置在后面的先执行。