struts2教程(7)--拦截器

栏目: Struts · 发布时间: 9年前

内容简介:struts2教程(7)--拦截器

Struts2 拦截器

一、拦截器介绍

拦截器 的使用 ,源自 Spring AOP (面向切面编程)思想

拦截器 采用 责任链 模式

在责任链模式里 , 很多对象由每一个对象对其下家的引用而连接起来形成一条链。

责任链每一个节点,都可以继续调用下一个节点,也可以阻止流程继续执行

struts2 中可以定义很多个拦截器,将多个拦截器按照特定顺序 组成拦截器栈 (顺序调用 栈中的每一个拦截器 )

1 struts2 所有拦截器 都必须实现 Interceptor 接口

package com.sihai.intercept;

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

public class _MyInterceptor implements Interceptor {

	public void destroy() {

	}

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

	public String intercept(ActionInvocation ai) throws Exception {

		System.out.println("my interceptor 拦截。。。。。");

		//return ai.invoke(); // 放行
	
		return Action.LOGIN;  //"login"
	}

}

2 AbstractInterceptor 类实现了 Interceptor 接口 . 并为 init, destroy 提供了一个空白的实现

package com.sihai.intercept;

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

public class MyInterceptor extends MethodFilterInterceptor {

	public void destroy() {

	}

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

	public String doIntercept(ActionInvocation ai) throws Exception {

		System.out.println("my interceptor 拦截。。。。。");

		//return ai.invoke(); // 放行
	
		return Action.LOGIN;  //"login"
	}

}

所有实际开发中,自定义拦截器 只需要 继承 AbstractInterceptor 类, 提供 intercept 方法实现

3 、 常用 struts2 拦截器

<interceptor-ref name="modelDriven"/> 模型驱动

<interceptor-ref name="fileUpload"/> 文件上传

<interceptor-ref name="params"> 参数解析封装

<interceptor-ref name="conversionError"/> 类型转换错误

<interceptor-ref name="validation"> 请求参数校验

<interceptor-ref name="workflow"> 拦截跳转 input 视图

二、自定义拦截器案例

案例 登陆,对其它 Action 访问  通过自定义拦截器 进行权限控制

导入 jar 包 ( struts2 jar c3p0 dbutils mysql 驱动)

web.xml

struts.xml

JDBCUtils 工具类

第一步 编写 index.jsp 提供 图书增删改查 四个功能

编写 BookAction ,提供四个业务方法

第二步 完成登陆功能

第三步 :必须要登陆 才能进行图书管理

使用 Filter 进行权限控制 ---- 过滤所有 web 请求 (所有 web 资源访问)

使用拦截器 进行权限控制 ---- 主要拦截对 Action 访问  (不能拦截 JSP

package com.sihai.intercept;

import org.apache.struts2.ServletActionContext;

import com.sihai.action.BookAction;
import com.sihai.domain.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class BookInterceptor extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {

		// 1.得到session中的user
		User user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("user");

		if (user == null) {
			BookAction action = (BookAction) invocation.getAction(); // 得到当前拦截的action对象。

			action.addActionError("权限不足,请先登录");// 存储错误信息

			return Action.LOGIN;
		}

		return invocation.invoke();
	}

}

定义拦截器 继承 AbstractInterceptor

配置拦截器

1、方式一

<!-- 注册拦截器 -->

<interceptors>

<interceptor name="privilege" class="com.sihai.interceptor.PrivilegeInterceptor"></interceptor>

</interceptors>

<action name="book_*" class="com.sihai.action.BookAction" method="{1}" >

<!-- 使用拦截器 -->

<!-- 当使用自定义拦截器 后,默认拦截器 就会失效  -->

<interceptor-ref name="defaultStack"></interceptor-ref>

<interceptor-ref name="privilege"></interceptor-ref>

</action>

2、方式二

<!-- 注册拦截器 -->

<interceptors>

<interceptor name="privilege" class="com.sihai.interceptor.PrivilegeInterceptor"></interceptor>

<!-- 自定义拦截器栈 -->

<interceptor-stack name="privilegeStack">

<interceptor-ref name="defaultStack"></interceptor-ref>

<interceptor-ref name="privilege"></interceptor-ref>

</interceptor-stack>

</interceptors>

<!-- 设置当前包 所有Action 都使用 自定义拦截器栈 -->

<default-interceptor-ref name="privilegeStack"></default-interceptor-ref>

struts.xml

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

<struts>
	<package name="default" namespace="/" extends="struts-default">
		

		<interceptors>

			<interceptor name="my" class="com.sihai.intercept.MyInterceptor">
			</interceptor>

			<interceptor name="bookInterceptor" class="com.sihai.intercept.BookInterceptor">
				<param name="includeMethods">add,update,delete</param>
			</interceptor>

			<interceptor-stack name="myStack">
				<interceptor-ref name="bookInterceptor"></interceptor-ref>
				<interceptor-ref name="defaultStack" />
			</interceptor-stack>
		</interceptors>
		
		<global-results>
			<result name="login">/login.jsp</result>
		</global-results>

		<action name="demo1" class="com.sihai.action.Demo1Action">
			<result name="login">/login.jsp</result>
			<!-- <interceptor-ref name="my" /> <interceptor-ref name="defaultStack"/> -->
			<interceptor-ref name="myStack" />
		</action>


		<action name="login" class="com.sihai.action.LoginAction">
			<result name="input">/login.jsp</result>

			<result>/book.jsp</result>
		</action>

		<action name="book_*" class="com.sihai.action.BookAction"
			method="{1}">
			<interceptor-ref name="myStack" />

		</action>
	</package>
</struts>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art and Science of CSS

The Art and Science of CSS

Jonathan Snooks、Steve Smith、Jina Bolton、Cameron Adams、David Johnson / SitePoint / March 9, 2007 / $39.95

Want to take your CSS designs to the next level? will show you how to create dozens of CSS-based Website components. You'll discover how to: # Format calendars, menus and table of contents usin......一起来看看 《The Art and Science of CSS》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具