在strtus2 中有一个比较重要的东西就是拦截器(Interceptors)

拦截器可以做到在已有的业务中插入一块共通的,比如在一个业务中,直接插入一串登录功能,就不用去每个页面一个个去显示是否登录,

直接在拦截器管理,就等于在总入口直接管理了,也不会出现,个别直接通过连接能进入网站的。

或者说,在没进入一个功能的时候记录一下客户的操作,这样与其在一个个的action分别记录,直接在拦截器去记录会更加的方便

或者是记录log

这里来简单讲解下拦截器的使用。

拦截器是在struts.xml中进行配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 请求参数的编码方式-->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开-->
<constant name="struts.action.extension" value="action"/>
<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.devMode" value="false"/>
<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 配置返回结果去哪里寻找 -->
<constant name="struts.convention.result.path" value="/" />
<!-- 配置类去哪里寻找 -->
<constant name="struts.convention.package.locators" value="action" />
<!-- 动态方法调用打开,可以通过感叹号调用action里面的方法 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- <package name="Menu" namespace="/Menu" extends="struts-default">
<action name="mainpage" class="com.almostman.action.MainPageAction">
<result name="success">/success.jsp</result>
<result name="input">/input.jsp</result>
</action>
</package> -->
<!-- 这里的{1}表示*的值 -->
<package name="Menu" namespace="/Menu" extends="struts-default"> <interceptors>
<!-- 定义拦截器
name:拦截器名称
class:拦截器类路径
-->
<interceptor name="timer" class="com.almostman.action.TimerInterceptor"></interceptor>
<interceptor name="logger" class="com.almostman.action.LoggerInterceptor"></interceptor>
<!-- 定义拦截器栈 -->
<interceptor-stack name="mystack">
<!-- 在有自定义拦截器的时候一定要将默认拦截器放在最上面,不然会出现参数无法传递 -->
<interceptor-ref name="defaultStack" />
<interceptor-ref name="timer"/>
<interceptor-ref name="logger"/>
</interceptor-stack>
</interceptors> <!-- 定义默认的拦截器 每个Action都会自动引用
如果Action中引用了其它的拦截器 默认的拦截器将无效 -->
<default-interceptor-ref name="mystack"></default-interceptor-ref>
<!-- 全局results配置 --> <action name="*" class="com.almostman.action.{1}Action" >
<result name="success">/{1}.jsp</result>
<result name="input">/input.jsp</result>
</action>
</package> <package name="Test" namespace="/Test" extends="struts-default">
<action name="HelloPage" class="com.almostman.action.HelloPageAction" >
<result name="success" type="velocity" >/HelloPage.vm</result>
<result name="input">/error.jsp</result>
</action>
</package> </struts>

这里自定义了两个拦截器的类,一个用来记录 用户操作的时间,一个用来记录log。

package com.almostman.action;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class TimerInterceptor extends AbstractInterceptor
{ @Override
public String intercept(ActionInvocation actionInvocation) throws Exception
{
// TODO Auto-generated method stub
System.out.println("在正确的时间登录"); return actionInvocation.invoke();
} }
package com.almostman.action;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class LoggerInterceptor extends AbstractInterceptor
{ public LoggerInterceptor()
{
// TODO Auto-generated constructor stub
} @Override
public String intercept(ActionInvocation actionInvocation) throws Exception
{
// TODO Auto-generated method stub
System.out.println("在这里输出log");
return actionInvocation.invoke();
} }

执行后一定要invoke出去,这样才能走到action,然后就是按照action的正常路程去返回给jsp页面。

,以上运行结果是:

能发下,拦截器生效,并且正常的走到了jsp的页面。

源码地址:http://pan.baidu.com/s/1cpdJdG

最新文章

  1. PHP 设置代码执行时间
  2. vi/vim 键盘图 &amp; 替换
  3. windows Android开发环境快速搭建和部署
  4. javascript prompt示例
  5. Route@书写规则的总结
  6. 纯CSS设置Checkbox复选框控件的样式
  7. libsvm工具箱C++编程实践2
  8. linux 下怎样查找一个文件夹在哪个目录下?
  9. Docker(七):Docker容器卷管理
  10. python——Django项目模板
  11. SPI知识总结
  12. TBschedule入门
  13. GreenDao的初次使用--号称Android最快的关系型数据库
  14. 微信小程序开发之初探
  15. RN-android 打包后,部分图片不显示
  16. 离线人脸识别C#类库分享 虹软2.0版本
  17. python vue 项目
  18. oracle截取字段中的部分字符串
  19. hashCode()方法 和 hash()方法
  20. windows7下搭建robot framework环境指导

热门文章

  1. javascript常用技巧归纳
  2. 解决 PowerDesigner 错误 The generation has been cancelled because errors have been found by the check model.
  3. [译]使用scikit-learn进行机器学习的简介(教程1)
  4. Vc++内存布局
  5. ssh-keygen配置
  6. 阅读《Android 从入门到精通》(24)——切换图片
  7. ARC和非ARC在项目中转换
  8. oracle查询表数据并重新插入到本表
  9. servlet awt随机图片验证码
  10. pandas如何去掉时间列的小时只保留日期