struts2 拦截器例子
来源:互联网 发布:xshell查看端口号 编辑:程序博客网 时间:2024/06/05 06:20
http://www.cnblogs.com/jackyrong/archive/2008/09/28/1301542.html
小结了struts2 中拦截器的一个例子,以方便快速进阶的.
比如有个很典型的例子,要求在编辑数据或者增加数据前,必须要先判断用户是否登陆.则我们先在src目录下,建一个
包strutsxml,专门用来存放关于struts的xml.
其中有个一个是基础xml,比如叫struts-base.xml
<package name="manager-default" namespace="/" extends="struts-default">
<!-- 定义全部的拦截器 -->
<interceptors>
<!-- 定义第一个自定义的拦截器,用于身份校验 -->
<interceptor name="isloginInterceptor" class="com.liao.IsLoginInterceptor"></interceptor>
<!-- 定义一个拦截器栈,系统所有Action都需配置 -->
<interceptor-stack name="islogin">
<interceptor-ref name="isloginInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<global-results>
<!-- 配置全局结果,用于身份校验结果返回 -->
<result name="none">/error/loginerror.jsp</result>
</global-results>
</package>
之后分别按功能模块定义xml,比如定义一个struts_crud.xml
<package name="crud" extends="manager-default" namespace="/adment">
<action name="a" class="aAction" method="abc">
<interceptor-ref name="params"/>
<interceptor-ref name="islogin"/>
<result>success.jsp</result>
</action>
这里表示首先要用islogin的拦截器了.
下面是写这个拦截器.让其继承abstractinterceptor
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.commons.logging.*;
import java.util.*;
public class IsLoginInterceptor extends AbstractInterceptor {
private static final Log logger = LogFactory.getLog(IsLoginInterceptor.class);
public String intercept(ActionInvocation invocation) throws Exception {
//取得请求相关的ActionContext实例
ActionContext act = invocation.getInvocationContext();
//获取Session
Map session = act.getSession();
//从Session中获取用户ID
String userId = (String)session.get("userId");
//判断用户是否登录
if(userId==null){
//未登录,返回重新登录
return Action.NONE;
}
//执行该拦截器的下一个拦截器,如没有,执行Action被请求的相应方法
return invocation.invoke();
}
}
- struts2 拦截器例子
- struts2拦截器简单例子
- struts2 中的拦截器例子快速进阶
- 学习struts2拦截器非常好的一个例子(转载)
- struts2中的拦截器概述和简单例子
- 【Struts2】Struts2中拦截器
- spring 拦截器例子
- spring 拦截器例子
- 拦截器应用例子
- struts2拦截器
- Struts2拦截器
- struts2.0拦截器
- struts2.0 拦截器
- struts2拦截器学习
- struts2 拦截器
- Struts2拦截器(Interceptor)
- struts2.0拦截器
- 用上struts2拦截器
- 关于mediaplayer
- loss functions for NN
- lintcode-组合-152
- mybatis 在oracle数据库中插入数据时获取自增ID
- java多线程 并发编程
- struts2 拦截器例子
- 推荐系统的那点事
- 如果你现在开始学习Android
- 转 hibernate事物的理解
- 使用工具书写代码
- Nginx安装及配置文件nginx.conf详解
- UICollectionView
- 13.GPUImage初探以及人脸检测
- 从何处开始阅读linux kernel源代码