定义拦截器有2种办法:
1.实现Interceptor接口
2.集成AbstractInterceptor抽象类

一、方法1

.....

<struts>
<package name="mypack" extends="struts-default">
<!--i. j. k.-->
<interceptors>
<interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"/> <global-results>
<result name="login" type="redirectAction">userAction_loginView</result>
</global-results> <!--不写method,111默认就是execute-->
<action name="indexAction" class="com.nf.action.IndexAction">
<result name="success">/WEB-INF/jsp/index.jsp</result> <interceptor-ref name="myStack"></interceptor-ref>
<!--
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
-->
</action> <action ......
</action>
</package> </struts>
package com.nf.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor; import java.util.Map; /*
定义拦截器有2种办法:
1.实现Interceptor接口
2.集成AbstractInterceptor抽象类
*/
public class LoginInterceptor implements Interceptor {
private Map<String,Object> session = null;
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation actionInvocation) throws Exception {
Object myAction = actionInvocation.getAction();
if(myAction instanceof UserAction){
System.out.println("你访问的Action是UserAction,不要校验Session,否则死循环");
//放行
return actionInvocation.invoke();
}else{
System.out.println("你访问的Action是:"+myAction);
} session = ActionContext.getContext().getSession();
Object user = session.get("user");
if (user!=null){
return actionInvocation.invoke();
}else{
return "login";
}
}
}

二、方法2

package com.nf.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; import java.util.Map; /*
定义拦截器有2种办法:
1.实现Interceptor接口
2.集成AbstractInterceptor抽象类
*/
public class LoginInterceptor extends MethodFilterInterceptor {
private Map<String, Object> session = null; protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
session = ActionContext.getContext().getSession();
Object user = session.get("user");
if (user!=null){
return actionInvocation.invoke();
}else{
return "login";
}
}
}
......
<struts>
<package name="mypack" extends="struts-default">
<!--i. j. k.-->
<interceptors>
<interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor">
<!--excludeMethods需要生效的话,
自定义的拦截器,不能使用实现Interceptor接口,
而是extends MethodFilterInterceptor
-->
<param name="excludeMethods">loginView,login</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"/> <global-results>
<result name="login" type="redirectAction">userAction_loginView</result>
</global-results> <!--不写method,111默认就是execute-->
<action name="indexAction" class="com.nf.action.IndexAction">
<result name="success">/WEB-INF/jsp/index.jsp</result> <!-- <interceptor-ref name="myStack"></interceptor-ref> -->
<!--
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
-->
</action> <action name="therFunctionAction" class="com.nf.action.OtherFunctionAction">
<!--不写name,222默认就是success-->
<result>/WEB-INF/jsp/otherFunction.jsp</result>
</action> <action.....
</action>
</package> </struts>

地址;https://gitee.com/MuNianShi/user5.git

Struts2学习-拦截器2续的更多相关文章

  1. Struts2学习---拦截器+struts的工作流程+struts声明式异常处理

    这一节我们来看看拦截器,在讲这个之前我是准备先看struts的声明式异常处理的,但是我发现这个声明式异常处理就是由拦截器实现的,所以就将拦截器的内容放到了前面. 这一节的内容是这样的: 拦截器的介绍 ...

  2. Struts2学习-拦截器2

    1.做一个登陆页面(loginView.jsp,才用Action来访问),2.登陆成功后,可以跳转到系统的首页(index.jsp),3.首页有一个链接(testOtherAction访问其它的功能模 ...

  3. Struts2学习-拦截器

    1.新建项目user4,建立好和user3一样的目录,与之相比只是添加几个类,主要是struts.xml和action类的改变,其结果没有太大的变化 struts,xml <?xml versi ...

  4. Struts2【拦截器】就是这么简单

    什么是拦截器 拦截器Interceptor.....拦截器是Struts的概念,它与过滤器是类似的...可以近似于看作是过滤器 为什么我们要使用拦截器 前面在介绍Struts的时候已经讲解过了,Str ...

  5. JavaWeb框架_Struts2_(三)---->Struts2的拦截器

    2. Struts2的拦截器(使用拦截器实现权限控制) 2.1 拦截器的概述 拦截器是Struts2的核心组成部分,它可以动态的拦截Action调用的对象,类似与Servlet中的过滤器.Struts ...

  6. (六)Struts2的拦截器

    一.简介 拦截器体系是struts2重要的组成部分.正是大量的内建拦截器完成了该框架的大部分操作. 比如params拦截器将请求参数解析出来,设置Action的属性.servletConfig拦截器负 ...

  7. 简单理解Struts2中拦截器与过滤器的区别及执行顺序

    简单理解Struts2中拦截器与过滤器的区别及执行顺序 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标 ...

  8. struts2总结六: Struts2的拦截器

    一.Struts2的系统结构图

  9. Struts2使用拦截器完成权限控制示例

    http://aumy2008.iteye.com/blog/146952 Struts2使用拦截器完成权限控制示例 示例需求:    要求用户登录,且必须为指定用户名才可以查看系统中某个视图资源:否 ...

随机推荐

  1. Covering(矩阵快速幂)

    Bob's school has a big playground, boys and girls always play games here after school.  To protect b ...

  2. HDU6393(LCA + RMQ + 树状数组) n边图,两点最短距离 , 修改边权

    这道题的进阶版本 进阶版本 题意: 一个n个点,n条边的图,2中操作,1是将某条边的权值更改,2是询问两点的最短距离. 题解: 由于n个点,n条边,所以是树加一个环,将环上的边随意取出一条,就是1颗树 ...

  3. 天梯赛easy题

    2 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> ...

  4. Win10如何新建用户怎么添加新账户

    https://jingyan.baidu.com/article/25648fc162d5899190fd0069.html 很多朋友都是安装完Windows10系统后,直接使用超级管理员账号登录系 ...

  5. ASP.NET与.NET区别

    1.NET是什么? .Net全称.NET Framework是一个开发框架,不是一门编程语言,简单的来说 就是一组类库框架,.NET开发支持C#.VB.NET.J#.Js和Managed C++等 其 ...

  6. Manacher算法(马拉车)

    学习博客:https://www.cnblogs.com/love-yh/p/7072161.html 首先,得先了解什么是回文串(我之前就不是很了解,汗).回文串就是正反读起来就是一样的,如“abb ...

  7. (转)linux shell单引号、双引号及无引号区别

    原文:http://blog.csdn.net/woshizhangliang999/article/details/50132265 3.描述linux shell中单引号.双引号及不加引号的简单区 ...

  8. IE Error: '__doPostBack' is undefined 问题解决

    突然遇到个很奇怪的BUG,翻页控件,其他浏览器一切正常,IE无法翻页,会提示 '__doPostBack' is undefined 后来搜索发现: [原文發表地址] Bug and Fix: ASP ...

  9. Java关键字final、static使用总结 (final static在容器中不可以改变容器但可以改变存放)

    一.final        根据程序上下文环境,Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量.你可能出于两种理解而需要阻止改变:设计或效 ...

  10. Eclipse的企业开发时常用快捷键使用、优化配置(博主推荐)

    不多说,直接上干货! 一.简介 eclipse可谓是Java开发界的神器,基本占据了大部分的Java开发市场,而且其官方还对其他语言提供支持,如C++,Ruby,JavaScript等等.为什么使用它 ...