【Struts2三】拦截器
来源:互联网 发布:淘宝天猫店多少钱 编辑:程序博客网 时间:2024/05/23 11:04
拦截器:就是在访问action之前,对其进行拦截!可以在拦截器中做一些逻辑的处理!比如权限验证,没有权限就不给予访问!
拦截器等效于servlet中的过滤器!
使用拦截器步骤:
1.定义自己的拦截器:
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* 定义自己的拦截器,需要实现Intercept接口!
*
*/
public class MyInterceptor implements Interceptor{
public void destroy() {
}
public void init() {
}
/**
* 重写intercept方法,在该方法中实现自己的拦截逻辑!
* 调用invocation.invoke()方法放行action!
*/
public String intercept(ActionInvocation invocation) throws Exception {
System. out.println("图片上传" );
return invocation.invoke();
// return null;//不执行action
}
}
2.在struts-interceptor.xml的配置文件中声明:
需要在struts.xml中包含上述配置文件!
struts-interceptor.xml:
<?xml version= "1.0" encoding ="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="interceptor" namespace="/" extends="struts-default" >
<!-- 声明拦截器,拦截器声明应该位于action之前-->
<interceptors>
<!--声明自己定义的拦截器 -->
<interceptor name= "imageInterceptor"
class= "cn.itheima03.struts2.interceptor.MyInterceptor" ></interceptor>
<!-- 声明拦截器栈 -->
<interceptor-stack name= "myInterceptor">
<interceptor-ref name="imageInterceptor" ></interceptor-ref>
<interceptor-ref name="defaultStack" ></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 修改默认的拦截器栈 -->
<default-interceptor-ref name="myInterceptor" ></default-interceptor-ref>
<!--声明action,在执行action之前,会先执行拦截器中的方法 -->
<action name= "interceptorAction_*" method ="{1}"
class= "cn.itheima03.struts2.interceptor.InterceptorTestAction" >
<result>
index.jsp
</result>
</action>
</package >
</struts>
3.定义action:
import com.opensymphony.xwork2.ActionSupport;
public class InterceptorTestAction extends ActionSupport{
public String interceptor(){
System. out.println("interceptor" );
return SUCCESS ;
}
}
0 0
- 【Struts2三】拦截器
- Struts2学习三拦截器
- Struts2.x教程(三) Struts2拦截器
- struts2学习(三)--拦截器
- struts2 18拦截器详解(三) --- AliasInterceptor
- JavaEE笔记(三)Struts2 拦截器
- struts2之三 拦截器机制
- Struts2拦截器-学习篇三
- Struts2第(三)篇《struts2拦截器源码分析》
- struts2(三)struts2拦截器源码分析
- 细谈struts2(三)struts2拦截器源码分析
- struts2的一些用法(三):3.拦截器的设置
- 学习struts2建bbs总结三:登录拦截器
- struts2框架(三)——拦截器
- struts2拦截器的三种实现方式
- struts2拦截器interceptor的三种配置方法
- struts2拦截器的三种配置方法
- struts2中拦截器实现的三种方式
- eclipse分析内存溢出的工具
- linux 服务器部署go工程
- solr安装ansj_seg分词
- [刷题]Fibonacci
- php登录的一个小实例
- 【Struts2三】拦截器
- viewstate和eventvalidation
- WEB_EasyUI中comboBox远程绑定json数据引起的访问跨域问题
- 单例设计模式之安全的懒汉式
- 学习+思考+总结+分享
- WebService
- win2008(64位系统)上部署.net程序连接oracle数据库注意事项
- MediaPlayer 常用方法介绍
- javaScript学习笔记2