5、Struts2自定义拦截器
一、拦截器相关知识
1、Struts2框架剖析

Holly版本生活案例:
影视公司(拍电影) ActionMapper
传媒公司(包装明星) ActionMapping
明星 Action
经纪人 ActionProxy(代理对象)
小工所在单位 ActionInvocation
小工 Interceptor(拦截器)
递归==99归一

2、struts2工作原理

3、拦截器工作原理

拦截器围绕着Action和Result的执行而执行。拦截器的工作原理类似递归的九九归一!
拦截器执行的三个阶段,有条件的执行周期:
(1)做一些Action执行前的预处理:拦截器可以准备、过滤、改变或者操作任何可以访问的数据,包括Action。
(2)调用ActionInvocation的invoke()方法将控制交给后续拦截器或返回结果字符串终止执行:如果拦截器决定请求的处理不应该继续,可以不调用invoke()方法,而是直接返回一个控制字符串。通估这种方式,可以停止后续的执行,并且决定哪个结果呈现给客户端。
(3)做一些Action执行后的处理:此时拦截器依然可以改变可以访问的对象和数据,只是此时框架已经选择了一个结果呈现给客户端了。
二、自定义拦截器
1、创建如下项目结构

2、在src下的com.action包下创建MyTimerAction.java
package com.action; import com.opensymphony.xwork2.Action; /**
* 记录执行时间的Action
* @author Holly老师
*
*/
public class MyTimerAction implements Action { public String execute() throws Exception {
System.out.println("Action记录执行时间");
return SUCCESS;
} }
MyTimerAction.java
3、在src下的com.interceptor包下创建自定义拦截器类MyTimerInterceptor.java
package com.interceptor; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* 自定义拦截器:
* 记录动作执行所花费的时间
* @author pc
*
*/
public class MyTimerInterceptor implements Interceptor{ public void destroy() {
System.out.println("销毁的方法...."); } public void init() {
System.out.println("初始化方法...."); } /**
* 拦截器执行的入门方法(小工的某个技能)
* @param invocation 经纪人
*/
public String intercept(ActionInvocation invocation) throws Exception {
//1.执行Action之前的工作:获取开始执行的时间
long startTime=System.currentTimeMillis();
System.out.println("执行Action之前的工作,开始时间"+startTime); //2.执行后续的拦截器或Action(接收经纪人的口令)
String result=invocation.invoke(); //3.执行Action之后的工作:计算并输出执行的时间
long endTime=System.currentTimeMillis(); long execTime=endTime-startTime; System.out.println("执行Action后的,结束时间"+endTime);
System.out.println("总共用时:"+execTime); //返回结果字符串 (经纪人口令给下一个小工)
return result; } }
MyTimerInterceptor .java
4、在src下创建struts.xml文件并配置拦截器
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" >
<struts>
<!-- 中文乱码处理 -->
<constant name="struts.i18n.encoding" value="UTF-8"/> <package name="default" namespace="/" extends="struts-default">
<!-- 配置所有拦截器的节点 -->
<interceptors>
<interceptor name="myTimer" class="com.interceptor.MyTimerInterceptor"></interceptor>
</interceptors> <!-- 配置Action=明星 -->
<action name="myTimer" class="com.action.MyTimerAction">
<result>/index.jsp</result>
<!-- 引用拦截器==小工 -->
<interceptor-ref name="myTimer"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
struts.xml
5、编辑WebRoot下的WEB-INF下的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> </web-app>
web.xml
6、运行项目

7、查看控制台的输出

5、Struts2自定义拦截器的更多相关文章
- Struts2自定义拦截器Interceptor以及拦截器登录实例
1.在Struts2自定义拦截器有三种方式: -->实现Interceptor接口 public class QLInterceptorAction implements Interceptor ...
- struts2自定义拦截器 设置session并跳转
实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆. 为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟 修改web.xml view plainco ...
- 12.Struts2自定义拦截器
12.自定义拦截器 拦截器是Struts2的一个重要特性.因为Struts2的大多数核心功能都是通过拦截器实现的. 拦截器之所以称之为“拦截器”,是因为它可以拦截Action方法的执行, ...
- 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】
一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...
- Struts2 自定义拦截器
自定义拦截器(权限管理),包含了对ajax和表单请求的拦截 package com.interceptor; import java.io.IOException; import java.io.Pr ...
- Struts2自定义拦截器
1. 需求 自定义拦截器实现,用户登录的访问控制. 2. 定义拦截器类 public class LoginInterceptor extends AbstractInterceptor { @Ove ...
- struts2自定义拦截器与cookie整合实现用户免重复登入
目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码 结构(两部份)=struts2.xml+自定义拦截器 ...
- Struts2自定义拦截器处理全局异常
今天在整理之前的项目的时候想着有的action层没有做异常处理,于是想着自定义拦截器处理一下未拦截的异常. 代码: package cn.xm.exam.action.safeHat; import ...
- Struts2自定义拦截器——完整实例代码
比如一个网上论坛过滤系统,将网友发表的不文明.不和谐的语言,通过拦截器对这些文字进行自动替代. 该项目包含: 1.自定义拦截器(MyInterceptor.java) 2.发表评论的页面(news.j ...
随机推荐
- oracle_sequence用法
1. About Sequences(关于序列) 序列是数据库对象一种.多个用户可以通过序列生成连续的数字以此来实现主键字段的自动.唯一增长,并且一个序列可为多列.多表同时使用.序列消除了串行化 ...
- digitalocean Vultr Linode 三家海外vps最新真实情况
中国有大批用户,在使用海外vps服务器.好处是不言而喻的:性价比高.带宽大.免备案.可搭梯子,没有后门监控. 有趣的是,每一年的周期观察,都能发现海外vps对中国大陆的线路速度.可用性变化.过去速度快 ...
- ViewController
ViewController ViewController 进入VC后先调用loadView,再调用ViewDidLoad ViewDidLoad皆为系统调用 需要先调用[super viewDidL ...
- Batch File Rename Utility(文件批量改名软件) 1.1.4231
软件名称: Batch File Rename Utility(文件批量改名软件) 1.1.4231.23098 软件语言: 英文 授权方式: 免费软件 运行环境: Win7 / Vista / Wi ...
- hdu1034
#include<stdio.h>const int MAXN=1000;int a[MAXN];int main(){ int n; int i; while(scanf("% ...
- 支付宝AR实景红包上线不久即遭破解,官方已提高技术门槛
临近春节,阿里巴巴和腾讯的红包大战可谓下足功夫,上周支付宝推出了AR实景红包,该玩法基于"LBS+AR+红包"的方式,类似与今年火爆全球的AR手游Pekomon Go ,只不过这次 ...
- svn checkout操作
svn checkout https://svn.com/svn/project 该操作从svn服务器上拉代码下来,并且建立本地和远端的文件对应,状态的关联. 1,和export的区别 svn检出操作 ...
- surface pro系统按键+重装系统
一. 如果无法进入系统: surface pro1代,可以插入U盘启动盘,然后按音量键上键,然后按电源键,然后释放电源键,然后等屏幕上"Surface"出来后,释放音量键. 2代以 ...
- Select In SQL Server-Cross Instance in same domain and different domain
Same Domain: Exec sp_addlinkedserver 'PC087':Add Remote Server Exec sp_dropserver 'InstcanceName':De ...
- 我对Map端spill的理解
一.先看简单理解 对于hadoop的map端配置项"mapreduce.task.io.sort.mb"和"mapreduce.map.sort.spill.percen ...