请求过滤
请求拦截
返回过滤
Struts2中拦截器与过滤器的执行顺序
通过上图可以看到,顺序应该是我们配的filter在前,之后是struts2的FilterDispatcher,然后是各个拦截器,最后原路返回。
下面我们来验证一下:
web.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app version="2.4"
-
xmlns="http://java.sun.com/xml/ns/j2ee"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
-
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
<welcome-file-list>
-
<welcome-file>login.jsp</welcome-file>
-
</welcome-file-list>
-
-
<filter>
-
<filter-name>MyFilter</filter-name>
-
<filter-class>filter.MyFilter</filter-class>
-
</filter>
-
<filter-mapping>
-
<filter-name>MyFilter</filter-name>
-
<url-pattern>/*</url-pattern>
-
</filter-mapping>
-
-
<filter>
-
<filter-name>struts2</filter-name>
-
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
-
</filter>
-
<filter-mapping>
-
<filter-name>struts2</filter-name>
-
<url-pattern>/*</url-pattern>
-
</filter-mapping>
-
-
</web-app>
struts.xml
-
<!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" extends="struts-default">
-
<interceptors>
-
<interceptor name="loginInterceptor" class="interceptor.LoginInterceptor" />
-
<interceptor-stack name="loginStack">
-
<interceptor-ref name="loginInterceptor" />
-
<interceptor-ref name="defaultStack" />
-
</interceptor-stack>
-
</interceptors>
-
<default-interceptor-ref name="loginStack"></default-interceptor-ref>
-
<global-results>
-
<result name="login">/login.jsp</result>
-
</global-results>
-
<action name="Login" class="action.LoginAction">
-
<result>/index.jsp</result>
-
</action>
-
</package>
-
</struts>
login.jsp
-
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
-
<html>
-
<head>
-
<title>My JSP 'index.jsp' starting page</title>
-
</head>
-
-
<body>
-
<h3>Login</h3>
-
<hr>
-
<form action="Login.action" method="POST">
-
<input type="submit" value="submit" />
-
</form>
-
</body>
-
</html>
index.jsp
-
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
-
<html>
-
<head>
-
<title>My JSP 'index.jsp' starting page</title>
-
</head>
-
-
<body>
-
<h3>Login Success!</h3>
-
</body>
-
</html>
filter.MyFilter
-
package filter;
-
-
import java.io.IOException;
-
-
import javax.servlet.Filter;
-
import javax.servlet.FilterChain;
-
import javax.servlet.FilterConfig;
-
import javax.servlet.ServletException;
-
import javax.servlet.ServletRequest;
-
import javax.servlet.ServletResponse;
-
-
public class MyFilter implements Filter {
-
-
public void destroy() {
-
}
-
-
public void doFilter(ServletRequest request, ServletResponse response,
-
-
-
chain.doFilter(request, response);
-
-
-
}
-
-
public void init(FilterConfig arg0) throws ServletException {
-
}
-
-
}
-
interceptor.LoginInterceptor
-
package interceptor;
-
-
import org.apache.struts2.ServletActionContext;
-
-
import com.opensymphony.xwork2.ActionInvocation;
-
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
-
-
public class LoginInterceptor extends AbstractInterceptor {
-
-
private static final long serialVersionUID = -2796326480994355318L;
-
-
@Override
-
-
-
if(!"YOYO".equals(ServletActionContext.getRequest().getParameter("username"))){
-
return "login";
-
}
-
-
-
-
return result;
-
}
-
-
}
action.LoginAction
部署好后打开login.jsp,输入任意非"YOYO"字符,控制台打印出
说明拦截之后还会按照原路返回;
如果输入"YOYO",则打印出
请求过滤
请求拦截
登陆ACTION
返回拦截
返回过滤
请求拦截
登陆ACTION
返回拦截
返回过滤
由此可以验证它的执行顺序 = =。