struts2教程(7)--拦截器
来源:互联网 发布:vue.js 视频教程下载 编辑:程序博客网 时间:2024/05/16 15:41
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 {@Overrideprotected String doIntercept(ActionInvocation invocation) throws Exception {// 1.得到session中的userUser 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>
- struts2教程(7)--拦截器
- Struts2拦截器教程
- Struts2.x教程(三) Struts2拦截器
- struts2教程- Interceptor(默认拦截器)介绍
- Struts2教程8:拦截器概述
- Struts2教程8:拦截器概述
- struts2教程- Interceptor(默认拦截器)介绍
- Struts2教程8:拦截器概述
- Struts2教程8:拦截器概述
- Struts2教程8:拦截器概述
- struts2教程- Interceptor(默认拦截器)介绍
- struts2教程:15、自定义拦截器
- Struts2教程8:拦截器概述
- Struts2教程8:拦截器概述
- Struts2教程8:拦截器概述
- Struts2教程8:拦截器概述
- Struts2教程8:拦截器概述
- Struts2 官方教程:介绍拦截器
- dagger
- 一个经典例子让你彻彻底底理解java回调机制
- aws
- Java线程:新特征-原子量
- 【设计模式】行为型模式——命令模式(Command Pattern)
- struts2教程(7)--拦截器
- 进阶软件测试工程师的必备
- Linux下cutecom使用USB转串口线
- [BZOJ1077][SCOI2008]天平(差分约束)
- dfs
- 让你的设计更加高效便捷的5款原型设计工具
- Monkey测试(压力测试)----真机测试步骤
- NAT(Network Address Translation)
- 基于busybox构建最小linux Docker镜像系统