struts2拦截器
来源:互联网 发布:清迈打车软件 编辑:程序博客网 时间:2024/06/08 05:08
拦截器:
三种方法实现拦截器
①实现接口
interceptor
②扩展类
package com.mynews.intercept;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class TimerIntercept extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
long start=System.currentTimeMillis();
String result=invocation.invoke();//继续执行下一个拦截器,后续拦截器执行完之后才继续向下执行
long end=System.currentTimeMillis();
System.out.println(end-start);
return result;
}
}
<package name="news" namespace="/news" extends="struts-default">
<!-- 声明一个拦截器 -->
<interceptors>
<interceptor name="timer" class="com.mynews.intercept.TimerIntercept"></interceptor>
<interceptor-stack name="mystack"><!-- 将拦截器放入栈中 -->
<interceptor-ref name="timer"></interceptor-ref> <!--自己定义的拦截器-->
<interceptor-ref name="defaultStack"></interceptor-ref><!-- 引入系统默认的拦截器 -->
</interceptor-stack>
</interceptors>
<!-- 将拦截器装备到action -->
<default-interceptor-ref name="mystack"></default-interceptor-ref>
<action name="*_NewsAction" class="com.mynews.action.NewsAction" method="{1}">
<result name="main" type="dispatcher">/ch01/main.jsp</result>
<!-- <result name="main" type="redirect">/ch01/main.jsp</result> 重定向方法 -->
</action>
</package>
③方法过滤拦截器
package com.mynews.intercept;
import java.util.Map;
import com.mynews.entity.Users;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class RoleIntercept extends MethodFilterInterceptor {
/**
* 方法拦截器
*/
private static final long serialVersionUID = 1L;
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
String result=null;
ActionContext context=invocation.getInvocationContext();
Map sessionMap=context.getSession();
Users user=(Users) sessionMap.get("users");
if(user==null){
/*
* ServletActionContext.getResponse().sendRedirect("指定页面");
* return null;
* */
return Action.LOGIN;
}else{
result=invocation.invoke();
}
return result;
}
}
<package name="news" namespace="/news" extends="struts-default">
<!-- 声明一个拦截器 -->
<interceptors>
<interceptor name="timer" class="com.mynews.intercept.TimerIntercept"></interceptor>
<!-- 方法拦截器 -->
<interceptor name="role" class="com.mynews.intercept.RoleIntercept">
<param name="excludeMethods">showNews,showNews2</param><!-- 不包含的方法 -->
<param name="includeMethods">searchNewsList</param><!-- 包含的方法 -->
</interceptor>
<interceptor-stack name="mystack"><!-- 将拦截器放入栈中 -->
<interceptor-ref name="timer"></interceptor-ref> <!--自己定义的拦截器-->
<interceptor-ref name="role"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref><!-- 引入系统默认的拦截器 -->
</interceptor-stack>
</interceptors>
<!-- 将拦截器装备到action -->
<default-interceptor-ref name="mystack"></default-interceptor-ref>
<!-- 全局的结果 -->
<global-results>
<result name="login">/ch01/login.jsp</result>
</global-results>
<action name="*_NewsAction" class="com.mynews.action.NewsAction" method="{1}">
<result name="main" type="dispatcher">/ch01/main.jsp</result>
<!-- <result name="main" type="redirect">/ch01/main.jsp</result> 重定向方法 -->
</action>
</package>
- 【Struts2】Struts2中拦截器
- struts2拦截器
- Struts2拦截器
- struts2.0拦截器
- struts2.0 拦截器
- struts2拦截器学习
- struts2 拦截器
- Struts2拦截器(Interceptor)
- struts2.0拦截器
- 用上struts2拦截器
- struts2拦截器简介
- Struts2:拦截器概述
- Struts2拦截器(Interceptor)
- struts2 拦截器
- 关于struts2拦截器
- Struts2的拦截器
- struts2拦截器
- Struts2拦截器总结
- 带OnMouseUp、OnMouseDown、OnMouseMove事件的TrackBar控件
- js简单实现div遮罩层
- opencv里面图像通道问题
- 初识Hadoop
- Ext3.4 带查询form的grid封装
- struts2拦截器
- BitmapFactory.decodeResource 获取图片宽与高
- css设置ul中li不能实现左对齐(针对非IE浏览器)
- Struts2基于注解的Action配置
- MIPI-技术浅谈
- 使用javac编译java文件和使用javah生成C/C++头文件 遇到找不到类的问题
- 设置progressbar进度条颜色
- 张西雨 你妹的——社会化媒体下个人信息安全的思考
- abstract class 和interface 有什么区别?