zoukankan      html  css  js  c++  java
  • struts2的拦截器

      Struts2框架的底层依赖的是XWork框架,XWork框架是命令模式的实现,提供了Action的管理,Result对象的处理以及最重要的组件“拦截器”(interceptor)。在Struts2接收request到完成响应response的过程中,struts2框架内部对功能上进行了封装,比如上传、数据验证,以及登录功能中对Action属性的赋值,都是拦截器在起作用。拦截器可以将组件进行松耦合开发。

      可以在项目中创建多个拦截器,为了对拦截器有效管理,可以将它们分组,形成拦截器栈,在struts-default.xml 中就有一个名为defaultStack拦截器栈。

      定义拦截器就要继承AbstractInterceptor类重写intercept()方法,进行业务处理,ActionInterceptor的invoke()主要功能就是执行Action中的内容,返回值就是result的逻辑名称。

      接下来举个栗子:

    先定义一个action,在struts.xml中声明为printUsername.action,result对应的页面自定义

     1 package com.hyp.controller;
     2 
     3 /**
     4  * @author hyp
     5  * Project name is LearnStruts
     6  * Include in com.hyp.controller
     7  * hyp create at 2018/7/14
     8  **/
     9 public class PrintUsername {
    10     private String username;
    11     public String execute()
    12     {
    13         System.out.println("PrintUsername execute()"+username);
    14         return "toPrintUsernameJSP";
    15     }
    16 
    17     public String getUsername() {
    18         return username;
    19     }
    20 
    21     public void setUsername(String username) {
    22         this.username = username;
    23     }
    24 }

    接下来定义一个interceptor类,继承于AbstractInterceptor,重写intercept方法。

    package com.hyp.interceptor;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    
    /**
     * @author hyp
     * Project name is LearnStruts
     * Include in com.hyp.interceptor
     * hyp create at 2018/7/14
     **/
    public class TheInterceptor extends AbstractInterceptor {
        @Override
        public String intercept(ActionInvocation actionInvocation) throws Exception {
            System.out.println("前");
            String resultValue=actionInvocation.invoke();
            System.out.println("后");
            return resultValue;
        }
    }

    在struts.xml文件中增加

     <package name="testtest" extends="struts-default">
            <interceptors>
            <interceptor name="theInterceptor" class="com.hyp.interceptor.TheInterceptor"/>
            <interceptor-stack name="theInterceptorStack">
                <interceptor-ref name="defaultStack"/>
                <interceptor-ref name="theInterceptor"/>
            </interceptor-stack>
        </interceptors>
            <action name="printUsername" class="com.hyp.controller.PrintUsername">
                <result name="toPrintUsernameJSP">/printUsername.jsp</result>
                <interceptor-ref name="theInterceptorStack"/>
            </action>
    
        </package>

    然后运行程序,在浏览器输入http://localhost:8080/printUsername.action?username=hyp,刷新后会在console中发现

    在action执行的前后执行输出,有点像面对切面编程。

  • 相关阅读:
    Teched最后两天下载,同时新加熊老的teched录像,请尽快下载。
    如何学习,牛人是否真牛?
    为什么我的脚本大多是支持IE环境
    SPS中提供的Blog
    teched2004最后一天下载,新增js的menu1.0下载
    asp+xml+js所作的文件管理器,完全仿xp风格,精彩下载不要错过。
    将业务系统数据库的数据显示在页面上并且作WebPart的跨页面连接
    Activity中UI框架基本概念
    Android学习笔记——Activity的启动和创建
    Mms模块ConversationList流程分析(2)
  • 原文地址:https://www.cnblogs.com/pingxin/p/p00005.html
Copyright © 2011-2022 走看看