struts2拦截器

拦截器:它是struts2的核心,它介于请求与Action之间,它帮助我们实现了模块化程序设计以及“插拔式“程序设计。

要书写一个拦截器类,要实现Interceptor接口:


一,实现Interceptor接口,实现他的三个方法:destroy;init;intercept(ActionInvocation argo)。

拦截器1:

package com.buaa.interceptor;

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

public class Inte1 implements Interceptor {

	public void destroy() {
		System.out.println("........destroy1()..........");

	}

	public void init() {
		System.out.println("...........init1().............");
	}

	public String intercept(ActionInvocation arg0) throws Exception {
		System.out.println("方法执行之前拦截1");
		String s = arg0.invoke();
		System.out.println("方法执行之后拦截1");
		return s;
	}

}

拦截器2:

package com.buaa.interceptor;

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

public class Inte2 implements Interceptor {

	public void destroy() {
		System.out.println("........destroy2()..........");

	}

	public void init() {
		System.out.println("...........init2().............");
	}

	public String intercept(ActionInvocation arg0) throws Exception {
		System.out.println("方法执行之前拦截2");
		String s = arg0.invoke();
		System.out.println("方法执行之后拦截2");
		return s;
	}

}

拦截器3:

package com.buaa.interceptor;


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

public class Inte3 implements Interceptor {

	public void destroy() {
		System.out.println("........destroy3()..........");

	}

	public void init() {
		System.out.println("...........init3().............");
	}

	public String intercept(ActionInvocation arg0) throws Exception {
		System.out.println("方法执行之前拦截3");
		String s = arg0.invoke();
		System.out.println("方法执行之后拦截3");
		return s;
	}

}



二,创建action类

package com.buaa.action;

import com.opensymphony.xwork2.ActionSupport;

public class ShowAction extends ActionSupport{

	@Override
	public String execute() throws Exception {
		System.out.println("..........show()..........");
		return "show";
	}

}



三,在struts.xml中,声明拦截器,且若要使用它,必须在相应的action中配置相应的拦截器。

拦截器栈:多个拦截器或拦截器栈的集合

 

Struts2中提供了很多种拦截器,可以实现很多功能 ,例如:上传下载,模型驱动,国际化。。。。

但是,一旦在struts.xml中配置了自定义的拦截器,那么默认的拦截器就无效了。


<?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>
	<package name="default" namespace="/" extends="struts-default">
		<!-- 声明拦截器  -->
		<interceptors>
			<interceptor name="inte1" class="com.buaa.interceptor.Inte1"></interceptor>
			<interceptor name="inte2" class="com.buaa.interceptor.Inte2"></interceptor>
			<interceptor name="inte3" class="com.buaa.interceptor.Inte3"></interceptor>
			<interceptor-stack name="sta1">
				<interceptor-ref name="inte1"></interceptor-ref>
				<interceptor-ref name="inte2"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		
		<action name="show" class="com.buaa.action.ShowAction">
			<!-- 拦截器的配置  -->
			<interceptor-ref name="sta1"></interceptor-ref>
			<interceptor-ref name="inte3"></interceptor-ref>	
			<result name="show">/show.jsp</result>
		</action>	
	</package>

</struts>

四、演示结果:



页面展示:


项目布局:




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值