struts2的拦截器(6):拦截器的执行顺序

index.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="login">
<s:textfield name="name" label="姓名"></s:textfield>
<s:password name="pass" label="密码"></s:password>
<s:submit value="登录"></s:submit>
</s:form>
</body>
</html>
welcome.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Object obj=session.getAttribute("name");
if(obj==null){
	response.sendRedirect("index.jsp");
}
%>
welcome ${name}... 
</body>
</html>
struts.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
    <package name="demo" extends="struts-default">
    
       <interceptors>
        <interceptor name="first" class="interceptor.FirstInterceptor">
            <param name="name">第一个拦截器</param>
        </interceptor>
        
        <interceptor name="second" class="interceptor.SecondInterceptor">
            <param name="name">第二个拦截器</param>
        </interceptor>
       </interceptors>
       
       <action name="login" class="action.LoginAction">
          <interceptor-ref name="first"></interceptor-ref>
          <interceptor-ref name="second"></interceptor-ref>
          <interceptor-ref name="defaultStack"></interceptor-ref>
          <result name="success">/welcome.jsp</result>
          <result name="error">/index.jsp</result>
       </action>
    </package>
</struts>
LoginAction.java :

public class LoginAction extends ActionSupport {
	private String name;
	private String pass;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	
	public String execute(){
		if(getName().equals("scott")&& getPass().equals("tiger")){
			Map<String,Object> session=ActionContext.getContext().getSession();
			session.put("name",getName());
			return "success";
		}
		return "error";
	}
}
FirstInterceptor.java :

public class FirstInterceptor extends AbstractInterceptor {
	private String name;
	
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println(name+"------Action执行之前------");
		String result=invocation.invoke();
		System.out.println(name+"------Action执行之后------");
		return result;
	}

}
SecondInterceptor.java :

public class SecondInterceptor extends AbstractInterceptor {
	private String name;
	
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println(name+"------Action执行之前------");
		String result=invocation.invoke();
		System.out.println(name+"------Action执行之后------");
		return result;
	}

}

结论:

在Action的方法执行之前,位于拦截器链前面的拦截器将先发生作用;在Action的方法执行之后,位于拦截器链前面的拦截器将后发生作用。

你可能感兴趣的:(Interceptor,执行顺序)