Struts2拦截器配置实例
拦截器介绍
拦截器 的使用 ,源自Spring AOP(面向切面编程)思想
拦截器 采用 责任链 模式
* 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。
* 责任链每一个节点,都可以继续调用下一个节点,也可以阻止流程继续执行
在struts2 中可以定义很多个拦截器,将多个拦截器按照特定顺序 组成拦截器栈 (顺序调用 栈中的每一个拦截器 )
1、 struts2 所有拦截器 都必须实现 Interceptor 接口
2、 AbstractInterceptor 类实现了 Interceptor 接口. 并为 init, destroy 提供了一个空白的实现
所有实际开发中,自定义拦截器 只需要 继承 AbstractInterceptor类, 提供 intercept 方法实现
3、 常用struts2 拦截器
模型驱动
文件上传
参数解析封装
类型转换错误
请求参数校验
拦截跳转 input 视图
自定义拦截器案例
案例 : 登陆,对其它Action访问 通过自定义拦截器 进行权限控制
导入jar包 (struts2 jar、c3p0、 dbutils、 mysql驱动)
web.xml
struts.xml
JDBCUtils 工具类
第一步 : 编写index.jsp 提供 图书增删改查 四个功能
编写BookAction ,提供四个业务方法
第二步: 完成登陆功能
第三步 :必须要登陆 才能进行图书管理
使用Filter 进行权限控制 —- 过滤所有web请求 (所有web资源访问)
使用拦截器 进行权限控制 —- 主要拦截对Action访问 (不能拦截JSP)
定义拦截器 继承AbstractInterceptor
配置拦截器
方式一
方式二
<!-- 设置当前包 所有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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<!-- Add packages here -->
<package name="user" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="bookInterceptor" class="com.action.BookInterceptor">
<param name="includeMethods">add,update,delete</param>
<param name="excludeMethods">search</param>
</interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="bookInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<global-results>
<result name="login" type="dispatcher">/login.jsp</result>
</global-results>
<action name="login" class="com.action.LoginAction">
<result name="success" type="redirect">/book.jsp</result>
</action>
<action name="book*" class="com.action.BookAction" method="{1}">
<interceptor-ref name="myStack"></interceptor-ref>
</action>
</package>
</struts>
需要注意的是
1、struts标签下只能有
Content Model : ((package | include | bean | constant)*, unknown-handler-stack?)
2、
Element : package
Content Model : (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, default-class-
ref?, global-results?, global-exception-mappings?, action*)
Element : interceptors
Content Model : (interceptor | interceptor-stack)+
3、还有就是interceptor的位置,要在action之前
默认的拦截器也不能忘了引入,
在对应的action里面引入拦截器
下面是BookInterceptor.java的内容,定义了拦截器对请求的操作
package com.action;
import org.apache.struts2.ServletActionContext;
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 arg0) throws Exception {
// TODO Auto-generated method stub
User user = (User) ServletActionContext.getRequest().getSession()
.getAttribute("user");
System.out.println("执行了这个拦截器");
if (user == null) {
ServletActionContext.getRequest().setAttribute("msg",
new String("权限不足,请您先登陆"));
return Action.LOGIN;
}
return arg0.invoke();
}
}
Struts2拦截器配置实例的更多相关文章
- Struts2 拦截器配置以及实现
@(Java ThirdParty)[Struts|Interceptor] Struts2 拦截器配置以及实现 Struts2的拦截器应用于Action,可以在执行Action的方法之前,之后或者两 ...
- Struts2拦截器配置
1. 理解拦截器 1.1. 什么是拦截器: 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AO ...
- 转载 - Struts2拦截器配置
出处:http://blog.csdn.net/axin66ok/article/details/7321430 目录(?)[-] 理解拦截器 1 什么是拦截器 2 拦截器的实现原理 拦截器的配置 使 ...
- struts2 拦截器配置
CheckLoginInterceptor.java---拦截器具体实现类: package com.sunhoo.hcpms.struts2.action.interceptors; import ...
- struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证
struts2.xml 内容 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...
- Struts2拦截器配置和使用
拦截器是Struts2最强大的特性之一,它是一种可以让用户在Action执行之前和Result执行之后进行一些功能处理的机制. 说到拦截器interceptor,就会想到过滤器filter: 过滤器f ...
- Struts2 拦截器配置及使用
在我的项目中有个需求,实现记录用户操作的系统日志,基于这个功能我首先想到的是Struts 的拦截器.配置一个全部Action都会拦截的拦截,写一个公用的服务.每当用户发送请求到Action 就记录相应 ...
- struts2拦截器interceptor的三种配置方法
1.struts2拦截器interceptor的三种配置方法 方法1. 普通配置法 <struts> <package name="struts2" extend ...
- Struts2拦截器原理以及实例
一.Struts2拦截器定义 1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 2. ...
随机推荐
- maven引入已经拥有的jar包
<!-- https://mvnrepository.com/artifact/jfree/jcommon --><dependency> <groupId> ...
- win7 远程桌面连接过程
背景:在公司日常工作中经常需要是用到远程桌面的连接,在内网环境下,远程桌面连接比qq更加方便!可以考虑外网的连接. 1 准备工作 这里我实验的另一台机器的ip:168.33.51.198,本机ip:1 ...
- Linux(7)chmod解析
在UNIX和Linux的操作系统中, 每个文件(文件夹也被看作是文件)都按读, 写, 运行设定权限 比如用ls -l或ll命令列文件表时, 得到如下输出: -rw-r--r-- 1 apple use ...
- factorOne cannot be&nb…
factorOne cannot be resolved or is not a field 现象描述: Eclipse的使用时会在代码处出现警告factorOne cannot be resolve ...
- 6-最基础的服务-es6写法
创建server.js 'use strict'; //http模块 var http = require('http'); //封装的方法 var handlers = require('./han ...
- 2017-6-4 CTF解题报告
1.签到题 附件 扫描二维码得到 ZCTF{WELCOME_TO_20-209} 2.阿斯克的秘密 从前有个叫做阿斯克的人,他写了一句话,聪明的你能明白他写的是什么吗? 附件 int a; while ...
- Spring框架集成mybatis框架的配置(笔记)
<!-- 0.注解扫描 --><!-- 1.导入外部文件 --><!-- 2.数据源 --><!-- 3.session Factory -->< ...
- code_smith生成实体类
- 干货--Excel的表格数据的一般处理和常用python模块。
写在前面: 本文章的主要目的在于: 介绍了python常用的Excel处理模块:xlwt,xlrd,xllutils,openpyxl,pywin32的使用和应用场景. 本文只针对于Excel表中常用 ...
- 虚拟机安装linux系统不能上网解决方法
周末闲来无事,用虚拟机安装了centos6.5系统,安装成功后发现不能连接网络,然后我就一脸蒙蔽了,无奈之下,只能百度查找问题,幸运的是,我还真找到了解决的方法,根据教程一步步操作,成功了!为了避免以 ...