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的方法执行之后,位于拦截器链前面的拦截器将后发生作用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值