拦截器的编写(十一)

	1. 编写拦截器,需要实现Interceptor接口,实现接口中的三个方法
		protected String doIntercept(ActionInvocation invocation) throws Exception {
			// 获取session对象
			User user = (User) ServletActionContext.getRequest().getSession().getAttribute("existUser");
			if(user == null){
				// 说明,没有登录,后面就不会执行了
				return "login";
			}
			return invocation.invoke();
		}
	
	2. 需要在struts.xml中进行拦截器的配置,配置一共有两种方式
		<!-- 定义了拦截器 第一种方式
		<interceptors>
			<interceptor name="DemoInterceptor" class="com.itheima.interceptor.DemoInterceptor"/>
		</interceptors>
		-->
		
		<!-- 第二种方式:定义拦截器栈 -->
		<interceptors>
			<interceptor name="DemoInterceptor" class="com.itheima.interceptor.DemoInterceptor"/>
			<!-- 定义拦截器栈 -->
			<interceptor-stack name="myStack">
				<interceptor-ref name="DemoInterceptor"/>
				<interceptor-ref name="defaultStack"/>
			</interceptor-stack>
		</interceptors>
		
		<action name="userAction" class="com.itheima.demo3.UserAction">
			<!-- 只要是引用自己的拦截器,默认栈的拦截器就不执行了,必须要手动引入默认栈 
			<interceptor-ref name="DemoInterceptor"/>
			<interceptor-ref name="defaultStack"/>
			-->
			
			<!-- 引入拦截器栈就OK -->
			<interceptor-ref name="myStack"/>
		</action>

一、方式一

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值