• 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:struts2拦截方法的拦截器


    <?xml version="1.0" encoding="GBK"?>
    <project name="struts" basedir="." default="">
        <property name="dist" value="classes"/>
        <property name="src" value="src"/>
        
        <path id="classpath">
            <fileset dir="lib">
                <include name="*.jar"/>
            </fileset>
            <pathelement path="${dist}"/>
        </path>
    
        <target name="compile" description="Compile all source code">
            <delete dir="${dist}"/>
            <mkdir dir="${dist}"/>
            <copy todir="${dist}">
                <fileset dir="${src}">
                    <exclude name="**/*.java"/>
                </fileset>        
            </copy>
            <javac destdir="classes" debug="true" includeantruntime="yes"
                deprecation="false" optimize="false" failonerror="true">
                <src path="${src}"/>
                <classpath refid="classpath"/>
            </javac>
        </target>
    
    </project>
    <?xml version="1.0" encoding="GBK"?>
    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
        <!-- 定义Struts 2的核心Filter -->
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <!-- 让Struts 2的核心Filter拦截所有请求 -->
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>错误页面</title>
    </head>
    <body>
        您不能登录!
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>登录页面</title>
    </head>
    <body>
    <h3>用户登录</h3>
    <s:form action="login">
        <s:textfield name="username" label="用户名"/>
        <s:password name="password" label="密码"/>
        <s:submit value="登录"/>
    </s:form>
    </body>
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>成功页面</title>
    </head>
    <body>
        您已经登录!
    </body>
    </html>
    <?xml version="1.0" encoding="GBK"?>
    <!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.i18n.encoding" value="GBK"/>
    <!-- 配置本系统所使用的包 -->
    <package name="lee" extends="struts-default">
        <!-- 应用所需使用的拦截器都在该元素下配置 -->
        <interceptors>
            <!-- 配置mySimple拦截器 -->
            <interceptor name="mySimple"
            class="org.crazyit.app.interceptor.MyFilterInterceptor">
                <!-- 为拦截器指定参数值 -->
                <param name="name">拦截方法的拦截器</param>
            </interceptor>
        </interceptors>
    
        <action name="login" class="org.crazyit.app.action.LoginAction">
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.jsp</result> 
            <!-- 配置系统的默认拦截器 -->
            <interceptor-ref name="defaultStack"/>
            <!-- 应用自定义的mySimple拦截器 -->
            <interceptor-ref name="mySimple">
                <!-- 重新指定name属性的属性值 -->
                <param name="name">改名后的拦截方法过滤拦截器</param>
                <!-- 指定execute方法不需要被拦截 -->
                <param name="excludeMethods">execute</param>
            </interceptor-ref>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
    </struts>
    package org.crazyit.app.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class LoginAction
        extends ActionSupport
    {
        private String username;
        private String password;
        // username的setter和getter方法
        public void setUsername(String username)
        {
            this.username = username;
        }
        public String getUsername()
        {
            return username;
        }
        // password的setter和getter方法
        public void setPassword(String password)
        {
            this.password = password;
        }
        public String getPassword()
        {
            return password;
        }
    
        public String execute() throws Exception
        {
            System.out.println("进入execute方法执行体..........");
            Thread.sleep(1500);
            if (getUsername().equals("crazyit.org")
                && getPassword().equals("leegang") )
            {
                return SUCCESS;
            }
            return ERROR;
        }
    }
    package org.crazyit.app.interceptor;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.*;
    import java.util.*;
    
    import org.crazyit.app.action.*;
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    // 拦截方法的拦截器,应该继承MethodFilterInterceptor抽象类
    public class MyFilterInterceptor
        extends MethodFilterInterceptor
    {
        // 简单拦截器的名字
        private String name;
        // 为该简单拦截器设置名字的setter方法
        public void setName(String name)
        {
            this.name = name;
        }
        // 重写doIntercept()方法,实现对Action的拦截逻辑
        public String doIntercept(ActionInvocation invocation)
            throws Exception
        {
            // 取得被拦截的Action实例
            LoginAction action = (LoginAction)invocation.getAction();
            // 打印执行开始的时间
            System.out.println(name + " 拦截器的动作---------"
                + "开始执行登录Action的时间为:" + new Date());
            // 取得开始执行Action的时间
            long start = System.currentTimeMillis();
            // 执行该拦截器的后一个拦截器,或者直接指定Action的被拦截方法
            String result = invocation.invoke();
            // 打印执行结束的时间
            System.out.println(name + " 拦截器的动作---------"
                + "执行完登录Action的时间为:" + new Date());
            long end = System.currentTimeMillis();
            // 打印执行该Action所花费的时间
            System.out.println(name + " 拦截器的动作---------"
                + "执行完该Action的时间为" + (end - start) + "毫秒");
            return result;
        }
    }
  • 相关阅读:
    开始准备AWS SysOps Administrator associate 认证考试,接下来的Blog都是准备过程中的知识点
    什么是 AWS Data Pipeline
    AWS WAF 的工作原理
    CloudTrail 的工作原理
    AngularJS尝鲜——快递运费计算
    IE UAF 漏洞(CVE-2012-4969)漏洞分析与利用
    学着分析CVE-2017-7269 -- IIS 6.0 WebDAV远程代码执行漏洞
    数据结构:实验六(单循环链表实现链式队列)
    EPS2.2 整数溢出漏洞分析到利用pwntools进行漏洞利用
    serialVersionUID作用
  • 原文地址:https://www.cnblogs.com/tszr/p/12366850.html
Copyright © 2020-2023  润新知