定义拦截器有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. HDU_2082 找单词 【母函数的应用】

    题目: 假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50 ...

  2. 04-树6 Complete Binary Search Tree (30 分)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  3. APP开发的三种模式

    Hybrid APP混合开发的一些经验和总结 APP开发的三种模式:Native App .web App.hybrid App 1.原生app 使用原生app (android或iOS)开发APP. ...

  4. Linq 与 Lambda 简单使用

    //Lambda表达式详解 //int //List<int> numbers = new List<int> {1,2,3,4,5,6,7,8,9 }; //var n = ...

  5. Nginx + Lua搭建文件上传下载服务

    收录待用,修改转载已取得腾讯云授权 最新腾讯云技术公开课直播,提问腾讯W3C代表,如何从小白成为技术专家?点击了解活动详情 作者 | 庄进发 编辑 | 迷鹿 庄进发,信息安全部后台开发工程师,主要负责 ...

  6. Linux mmap 要主动释放共享内存

    #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/sta ...

  7. python 爬虫系列04-电影天堂连接爬虫

    学习的第四个爬虫 from lxml import etree import requests BASE_D = 'http://www.dytt8.net' headers = { 'User-Ag ...

  8. cxf 框架 webservice

    cxf 内置了一个web服务器 cxf简单入门实例 package test; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import c ...

  9. (三)TestNG

    1.testNG的部分注解 test测试方法都是执行顺序:并不是从上往下执行的,而是根据方法名ASCII码进行执行的,小的先执行 比如a比b先执行,1比2先执行,不管代码放的顺序是怎么样. impor ...

  10. 很有用的PHP笔试题系列三

    1. 什么事面向对象?主要特征是什么? 面向对象是程序的一种设计方式,它利于提高程序的重用性,使程序结构更加清晰.主要特征:封装.继承.多态. 2. SESSION 与 COOKIE的区别是什么,请从 ...