struts2自定义拦截器

配置struts.xml

<?xml version="1.0"encoding="gbk"?>

<!DOCTYPE struts PUBLIC

       "-//Apache SoftwareFoundation//DTD Struts Configuration 2.0//EN"

      "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

   <!-- 设置Web应用的默认编码集为gbk-->

   <constant name="struts.i18n.encoding"value="gbk" />

   <!-- 设置Web应用的默认Localezh_CN-->

   <constant name="struts.locale" value="zh_CN"/>

   <!-- 设置Struts2.1应用的国际化资源文件,多个文件中间可用逗号分隔 -->

   <constantname="struts.custom.i18n.resources"value="MessageResource,globalMessage" />

   <!-- 设置Struts2.1应用是否处于开发模式,通常在开发调试阶段设为true,正式上线后可设为false -->

   <constant name="struts.devMode" value="true"/>

   <!-- 设置Struts2.1的默认主题为simple -->

   <constant name="struts.ui.theme"value="simple" />

   <!-- 设置上传文件临时文件夹 -->

   <constant name="struts.multipart.saveDir"value="/tmp" />

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

        <interceptors>

            <interceptorname="Michael" class="com.intercepter.MichaelIntercepter">              <paramname="name">micael</param><!--配置拦截器名字 -->

           </interceptor>

        </interceptors>

       <action name="login"class="com.book.struts.action.LoginAction"> 

            <result>/WEB-INF/jsp/success.jsp</result>

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

               <interceptor-refname="Michael"> 

            <paramname="name">micahel拦截器</param>

          </interceptor-ref>

       </action>

   

   </package>

</struts>

 

<!-----------------------------配置自定义拦截器------------------------------->

package com.intercepter;

 

import java.util.Date;

 

importcom.book.struts.action.LoginAction;

importcom.opensymphony.xwork2.ActionInvocation;

importcom.opensymphony.xwork2.interceptor.Interceptor;

 

public class MichaelIntercepter implementsInterceptor {

 

   private String name;

 

   publicvoid destroy() {

 

   }

 

   publicvoid init() {

   }

 

   publicString intercept(ActionInvocation actionInvocation) throwsException {

       // 取得被拦截的Action

       LoginAction la =(LoginAction)actionInvocation.getAction();

       //设置Action的属性

       la.setName("u0");

       la.setAge("20");

       

       System.out.println("Action执行之前"+name +"拦截器的动作"+newDate());

       longstartime = System.currentTimeMillis();

       

       System.out.println("execute方法开始执行................");

       // 如果该拦截器之后没有其他拦截器,在struts.xml 配置Action 中如果没有指定方法则执行execute()

       //,如果有则执行指定的方法

       Stringresult = actionInvocation.invoke();

       //action执行结束时间

       System.out.println("Action执行之后"+name +"拦截器的动作"+newDate());

       longendtime = System.currentTimeMillis();

       

       System.out.println("执行Action需要"+(endtime-startime)+"毫秒");

       returnresult;

   }

 

   publicString getName() {

       returnname;

   }

 

   publicvoid setName(String name) {

       this.name = name;

   }

 

}

 

<!--------------------------------------配置Action--------------------------------->

package com.book.struts.action;

 

importcom.opensymphony.xwork2.ActionSupport;

 

public class LoginAction extends ActionSupport{

 

   private String name;

   private String age;

 

   publicString login() {

       returnSUCCESS;

   }

 

   publicString getName() {

       returnname;

   }

 

   publicvoid setName(String name) {

       this.name = name;

   }

 

   publicString getAge() {

       returnage;

   }

 

   publicvoid setAge(String age) {

       this.age = age;

   }

 

}

 

<!-----------------------------配置成功页面------------------------------------>

<%@ page language="java"import="java.util.*"pageEncoding="GB18030"%>

<%@taglib prefix="s"uri="/struts-tags"%>

<%

String path = request.getContextPath();

String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

 <head>

   <basehref="<%=basePath%>">

   

   <title>My JSP 'regSuccess.jsp'starting page</title>

   

   <meta http-equiv="pragma"content="no-cache">

   <meta http-equiv="cache-control"content="no-cache">

   <meta http-equiv="expires"content="0">   

   <meta http-equiv="keywords"content="keyword1,keyword2,keyword3">

   <meta http-equiv="description" content="Thisis my page">

   <!--

   <link rel="stylesheet" type="text/css"href="styles.css">

   -->

 

 </head>

  

 <body>

   <s:propertyvalue="name"/><br/>

   <s:property value="age"/>

 </body>

</html>

 

<!-------------------------完成部署项目------------------------------->

访问:http://localhost:8080/Books/michael/login

如果没有出错,在后台会输出

struts2自定义拦截器

 

测试一下拦截器执行的顺序

 

修改struts.xml 添加

<interceptor-refname="Michael">

  <paramname="name">micahe2拦截器</param>

</interceptor-ref>

 

访问:http://localhost:8080/Books/michael/login

如果没有出错,在后台会输出

 struts2自定义拦截器


从图中可以看出拦截器的执行顺序为 :在Action的控制方法执行之前,位于拦截器链前面的拦截器将先起作用

,在action的控制方法执行之后,位于拦截器链前面的拦截器将后起作用。

自定义拦截器的实现,应该实现com.opensymphony.xwork2.interceptor.Interceptor接口,该接口有三个方法

1.voiddestory();//销毁该拦截器之前的回调方法

2.void init();//初始化该拦截器的回调方法

3.String intercept(ActionInvocationinvocation);//拦截器实现拦截的逻辑方法,如果该方法直接返回一个字符串,系统将会跳转到该逻辑视图对应的实际视图资源,不会调用被拦截的action。


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值