struts2自定义拦截器
来源:互联网 发布:e63诺基亚怎么下载软件 编辑:程序博客网 时间:2024/06/11 02:57
首先自己写一个类来继承Interceptor(这里继承了MethodFilterInterceptor以实现对需要拦截的方法的配置)
public class MyIntercepter extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation invocation) throws Exception { //得到session中的user User user=(User) ServletActionContext.getRequest().getAttribute("user"); if(user==null){ return Action.LOGIN; } return invocation.invoke(); }}
在struts.xml中进行配置
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <!-- 是否启用开发模式,更改该文件之后不需要重新启动服务器 --> <constant name="struts.devMode" value="true" /> <!-- 启动动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- 这里的package和java中的包一样 , namespace是访问路径(不写的话默认是:namespace="") --> <package name="default" extends="struts-default" namespace="/"> <interceptors> <interceptor name="bookInterceptor" class="com.wz.intercepter.MyIntercepter"> <!-- 不拦截的方法 --> <param name="excludeMethods">select</param> <!-- 拦截的方法 --> <param name="includeMethods">delete,update,add</param> </interceptor> <interceptor-stack name="myInterceptors"> <interceptor-ref name="bookInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <action name="book_*" class="com.wz.action.BookAction" method="{1}"> <result name="login"> /login.jsp </result> <interceptor-ref name="myInterceptors"></interceptor-ref> </action> </package></struts>
被拦截的action
public class BookAction extends ActionSupport { public String add(){ System.out.println("add"); return null; } public String delete(){ System.out.println("delete"); return null; } public String update(){ System.out.println("update"); return null; } public String select(){ System.out.println("select"); return null; }}
0 0
- struts2 自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- Struts2---自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- Struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- struts2自定义拦截器
- 64-TCP/UDP 混合服务器
- 企业级云管理平台的架构实现与落地实践、趋势分析
- LCS相关问题
- 11款Linux下的数据恢复工具
- c#后台直接发起http请求
- struts2自定义拦截器
- Java并发编程实战
- UIButton 设置阴影效果
- 使用花生壳、路由器、个人电脑进行站点部署并实现外网域名访问图文详解(花生壳设置方法 使用方法 使用教程)
- sphinx 增量索引 实现近实时更新
- 学习笔记| AS入门(二) 简单控件篇(上)
- 线索二叉树(Threaded BinaryTree)
- 学习TP框架(一)
- AOJ-problem-849