zoukankan      html  css  js  c++  java
  • struts2基础——自定义拦截器

    一、自定义拦截器

    默认的拦截器能实现的功能是有限的,Struts2 支持自定义拦截器。

    二、拦截器类

    1.实现 Interceptor 接口

    2.继承 AbstractInterceptor 抽象类,需要实现 public String intercept(ActionInvocation actionInvocation) 方法,其中通过 actionInvocation.invoke() 继续调用后续拦截器 和 Action 方法。

    Struts2 会自动跳转到自定义拦截器的 interceptor 方法返回值对应的 result,如果直接返回一个 String,那么会将控制器交给目标 action 对应的 result。

    3.注册自定义拦截器与使用

    (1)Action 级

    <package name="default" namespace="/" extends="struts-default">
      <interceptor name="myInterceptor" class="com.nucsoft.struts.interceptor.MyInterceptor"/>
      <action name="interceptor" class="com.nucsoft.struts.token.InterceptorAction">
        <interceptor-ref name="myInterceptor"/>
        <interceptor-ref name="defaultStack"/>
          <result>/success.jsp</result>
          <result name="input">/error.jsp</result>
      </action>
    </package>

    (2)package 级

    <package name="default" namespace="/" extends="struts-default">
      <interceptors>
        <interceptor name="myInterceptor" class="com.nucsoft.struts.interceptor.MyInterceptor"/>
        <interceptor-stack name="myInterceptorStack">
          <interceptor-ref name="myInterceptor"/>
          <interceptor-ref name="defaultStack"/>
        </interceptor-stack>
      </interceptors>
      <default-interceptor-ref name="myInterceptorStack"/>
      <action name="myInterceptor" class="com.nucsoft.struts.token.InterceptorAction" method="myInterceptor">
        <result>/success.jsp</result>
        <result name="input">/error.jsp</result>
      </action>
    </package>
  • 相关阅读:
    寒假学习进度-14(疫情)
    寒假学习进度-13(Python自然语言处理)
    寒假学习进度-12(热词展示)
    寒假学习进度-11(词云图的使用)
    寒假学习进度-10(pyecharts的下载和使用)
    寒假学习进度-9(spark streaming编程初级实践)
    寒假学习进度-8(热词爬取)
    寒假学习进度-7(Python爬虫)
    寒假学习进度-6(Python连接MySQL数据库)
    寒假学习进度-5
  • 原文地址:https://www.cnblogs.com/solverpeng/p/5666585.html
Copyright © 2011-2022 走看看