struts2实验5:struts2 拦截器

实验思路

  1. 编写Interceptor 继承 AbstractInterceptor类
  2. 重写public String intercept(ActionInvocation invocation)方法
  3. 编写struts2.xml

关键struts2.xml 代码

<interceptors>
                <interceptor name="MyInterceptor" class="com.zy.javaee.interceptor.MyInterceptor">
                    <!--指定当前方法拦截器对象要进行拦截的方法对象-->
                    <!--<param name="includeMethods">doFirst,doSecond</param>-->
                    <!-- 指定对doThird方法不进行过滤操作 -->
                     <!--<param name="excludeMethods">doThird</param> -->
                    <param name="myInterceptorParam">my first interceptor</param>
                </interceptor>
 </interceptors>           
复制代码
<interceptor-ref name="MyInterceptor">
                    <param name="defaultUsername">testuser</param>
                    <param name="defaultPassword">123</param>
                    <param name="myInterceptorParam">my changed interceptor</param>
</interceptor-ref>

复制代码

目录结构

实验结果

代码(关键代码,文末有实验5github链接)

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
            "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
        <constant name="struts.devMode" value="true" />
        <constant name="struts.i18n.encoding" value="GBK"/>
        <constant name="struts.action.extension" value="do,action,"/>
        <constant name="struts.serve.static.browserCache" value="false"/>
        <constant name="struts.configuration.xml.reload" value="true"/>

        <!-- ################################   国际化重点 -Start-  ################################-->
        <constant name="struts.custom.i18n.resources" value="mess"/>
        <!-- ################################   国际化重点  -End- ################################-->

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

            <interceptors>
                <interceptor name="MyInterceptor" class="com.zy.javaee.interceptor.MyInterceptor">
                    <!--指定当前方法拦截器对象要进行拦截的方法对象-->
                    <!--<param name="includeMethods">doFirst,doSecond</param>-->
                    <!-- 指定对doThird方法不进行过滤操作 -->
                     <!--<param name="excludeMethods">doThird</param> -->
                    <param name="myInterceptorParam">my first interceptor</param>
                </interceptor>
            </interceptors>
            <global-results>
                <result name="exception" >/error.jsp</result>
            </global-results>
            <global-exception-mappings>
                <exception-mapping result="exception" exception="java.lang.Exception">
                </exception-mapping>
            </global-exception-mappings>
            <action name="tryException" class="com.zy.javaee.controller.actions.ErrorProcessAction" method="tryException">
                <result name="success">/users/login_userLoginPage.do</result>
            </action>

        </package>
        <package name="default" namespace="/" extends="struts-global">
            <action name="login">
                <result type="redirect">/users/login_userLoginPage</result>
            </action>
        </package>
        <package name="admin" namespace="/admin" extends="struts-global">
            <action name="login_*" class="com.zy.javaee.controller.actions.AdminLoginAction" method="{1}">
                <result name="success">${directUrl}</result>
                <param name="defaultAdmin.username">testadmin</param>
                <param name="defaultAdmin.password">123</param>
            </action>
        </package>

        <package name="users" namespace="/users" extends="struts-global">
            <action name="login_*" class="com.zy.javaee.controller.actions.UserLoginAction" method="{1}">

                <interceptor-ref name="MyInterceptor">
                    <param name="defaultUsername">testuser</param>
                    <param name="defaultPassword">123</param>
                    <param name="myInterceptorParam">my changed interceptor</param>
                </interceptor-ref>
                <result name="success">${directUrl}</result>

            </action>
        </package>
    </struts>
复制代码

MyInterceptor.java

package com.zy.javaee.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.zy.javaee.controller.actions.UserLoginAction;
import com.zy.javaee.domin.User;

import java.util.Date;
import java.util.Map;

/**
 * Created by Joey_Tsai on 2018/4/11.
 */
public class MyInterceptor extends  AbstractInterceptor {
    private String myInterceptorParam;
    private String defaultUsername;

    public  String getDefaultPassword() {
        return defaultPassword;
    }

    public void setDefaultPassword(String defaultPassword) {
        this.defaultPassword = defaultPassword;
    }

    private String defaultPassword;

    public String getDefaultUsername() {
        return defaultUsername;
    }

    public void setDefaultUsername(String defaultUsername) {
        this.defaultUsername = defaultUsername;
    }

    //    private String defaultPassword;
    public String getMyInterceptorParam() {
        return myInterceptorParam;
    }

    public void setMyInterceptorParam(String myInterceptorParam) {
        this.myInterceptorParam = myInterceptorParam;
    }

    //省略getter和setter
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        // TODO Auto-generated method stub
        Map paramMap = invocation.getInvocationContext().getParameters();
        String[] username = (String[]) paramMap.get("user.username");
        String[] password = (String[]) paramMap.get("user.password");
        String result;
        if (username != null && password != null){
            String[] usernameParam = username[0].split(",");
            String[] passwordParam = password[0].split(",");
            User user = new User();
            user.setUsername(usernameParam[0]);
            user.setPassword(passwordParam[0]);
            User defaultUser = new User();
            defaultUser.setUsername(defaultUsername);
            defaultUser.setPassword(defaultPassword);
            UserLoginAction userLoginAction = (UserLoginAction) invocation.getAction();
            userLoginAction.setUser(user);
            userLoginAction.setDefaultUser(defaultUser);
            System.out.println(myInterceptorParam + " 拦截器---开始执行登录Action的时间为" + new Date());
            long start = System.currentTimeMillis();
            result = invocation.invoke();
            System.out.println(myInterceptorParam + " 拦截器---执行完登录Action的时间为" + new Date());
            long end = System.currentTimeMillis();
            System.out.println(myInterceptorParam + " 拦截器---执行Action的时间为" + (end - start) + "ms");

        }
        else result = invocation.invoke();
        return result;
    }



}
复制代码

GITHUB链接:https://github.com/joeytsai03/javaee_test5.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值