你好,游客 登录 注册 搜索
背景:
阅读新闻

Struts2基础——自定义拦截器

[日期:2016-07-13] 来源:Linux社区  作者:solverpeng [字体:大 中 小]

一、自定义拦截器

默认的拦截器能实现的功能是有限的,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>

推荐阅读:

Struts中异步传送XML和JSON类型的数据 http://www.linuxidc.com/Linux/2013-08/88247.htm

Struts2的入门实例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2学习笔记-Value Stack(值栈)和OGNL表达式  http://www.linuxidc.com/Linux/2015-07/120529.htm 

struts2文件上传(保存为BLOB格式) http://www.linuxidc.com/Linux/2014-06/102905.htm

Struts2的入门实例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2实现ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm

遇到的Struts2文件下载乱码问题 http://www.linuxidc.com/Linux/2014-03/98990.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

Struts2 注解模式的几个知识点 http://www.linuxidc.com/Linux/2013-06/85830.htm

Struts 的详细介绍:请点这里
Struts 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133114.htm

linux
相关资讯       Struts2拦截器  Struts2自定义拦截器