struts2自定義攔截器

配置struts.xml

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

<!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//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應用的國際化資源文件,多個文件中間可用逗號分隔 -->

    <constant name="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>

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

             </interceptor>

         </interceptors>

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

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

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

                   <interceptor-ref name="Michael"> 

             <param name="name">micahel攔截器</param>

           </interceptor-ref>

        </action>

   

    </package>

</struts>

 

<!-----------------------------配置自定義攔截器------------------------------->

package com.intercepter;

 

import java.util.Date;

 

import com.book.struts.action.LoginAction;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

 

public class MichaelIntercepter implements Interceptor {

 

    private String name;

 

    public void destroy() {

 

    }

 

    public void init() {

    }

 

    public String intercept(ActionInvocation actionInvocation) throws Exception {

        // 取得被攔截的Action

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

        //設置Action的屬性

        la.setName("u0");

        la.setAge("20");

       

        System.out.println("Action執行之前"+name + "攔截器的動作"+new Date());

        long startime = System.currentTimeMillis();

       

        System.out.println("execute方法開始執行................");

        // 如果該攔截器之後沒有其他攔截器,在struts.xml 配置Action 中如果沒有指定方法則執行execute()

        //,如果有則執行指定的方法

        String result = actionInvocation.invoke();

        //action執行結束時間

        System.out.println("Action執行之後"+name + "攔截器的動作"+new Date());

        long endtime = System.currentTimeMillis();

       

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

        return result;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

}

 

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

package com.book.struts.action;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class LoginAction extends ActionSupport {

 

    private String name;

    private String age;

 

    public String login() {

        return SUCCESS;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getAge() {

        return age;

    }

 

    public void 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//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=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="This is my page">

    <!--

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

    -->

 

  </head>

  

  <body>

    <s:property value="name"/><br/>

    <s:property value="age"/>

  </body>

</html>

 

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

訪問:http://localhost:8080/Books/michael/login

如果沒有出錯,在後臺會輸出

struts2自定義攔截器

 

測試一下攔截器執行的順序

 

修改struts.xml 添加

<interceptor-ref name="Michael">

  <param name="name">micahe2攔截器</param>

</interceptor-ref>

 

訪問:http://localhost:8080/Books/michael/login

如果沒有出錯,在後臺會輸出

 struts2自定義攔截器


從圖中可以看出攔截器的執行順序爲 :在Action的控制方法執行之前,位於攔截器鏈前面的攔截器將先起作用

,在action的控制方法執行之後,位於攔截器鏈前面的攔截器將後起作用。

自定義攔截器的實現,應該實現com.opensymphony.xwork2.interceptor.Interceptor接口,該接口有三個方法

1.void destory();//銷燬該攔截器之前的回調方法

2.void init(); //初始化該攔截器的回調方法

3.String intercept(ActionInvocation invocation);//攔截器實現攔截的邏輯方法,如果該方法直接返回一個字符串,系統將會跳轉到該邏輯視圖對應的實際視圖資源,不會調用被攔截的action。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章