2019独角兽企业重金招聘Python工程师标准>>>

jsp页面

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
${message}

<form action="login.action" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="text" name="passoword"><br>
<input type="submit" value="提交">
</form>
</body>
</html>

Action方法

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class InterAction extends ActionSupport {

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("123");
return super.execute();
}

}
下面写自己的interceptor

package com.interceptor;
import model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class FirstInterceptor implements Interceptor {
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("FirstInterceptor is destroyed...");
}
@Override
public void init() {
// TODO Auto-generated method stub
       System.out.println("FirstInterceptor is inited ....");
}

@Override
public String intercept(ActionInvocation arg0) throws Exception {
// TODO Auto-generated method stub
Object u=ActionContext.getContext().getSession().get("user");
if(u==null)
{
ActionContext.getContext().getSession().put("message","重新登录");<!--把参数返回给jsp页面-->
return "fail";
}
return arg0.invoke();
}
}

struts.xml

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="firstInterceptor" class="com.interceptor.FirstInterceptor">
</interceptor>
<!-- 加到自己设置的拦截器栈里边去 -->
<interceptor-stack name="myStack">
<interceptor-ref name="firstInterceptor">
</interceptor-ref>
<interceptor-ref name="defaultStack">
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack">
</default-interceptor-ref>
<action name="login" class="com.action.InterAction">
<result name="success">/successLogin.jsp</result>
<result name="fail">/index.jsp</result>
</action>
</package>
</struts>

这样便可以简单实现自己的拦截器了

转载于:https://my.oschina.net/u/1254855/blog/171189

struts2实现拦截器的方法相关推荐

  1. struts2自定义拦截器并配置拦截器使其生效

    首先编写一个struts2的拦截器,要继承 MethodFilterInterceptor 并获取其中的方法 package star.july.d_interceptor;import com.op ...

  2. (转)Struts2的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/68648101 Struts2的拦截器 拦截器的概述 拦截器,在AOP(Aspect-Orien ...

  3. Struts2之拦截器篇

    拦截器是Struts2框架的核心和基础,Struts2绝大多数功能都是通过拦截器来完成的,当StrutsPrepareAndExecuteFilter拦截到用户请求后,大量拦截器会对该请求进行处理,然 ...

  4. Struts2 自定义拦截器

    所属栏目:AbstractInterceptor 如果要编写自定义拦截器,那么可以只用实现com.opensymphony.xwork2.interceptor.Interceptor这个接口即可. ...

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

    2019独角兽企业重金招聘Python工程师标准>>> 什么是拦截器 拦截器Interceptor.....拦截器是Struts的概念,它与过滤器是类似的...可以近似于看作是过滤器 ...

  6. java中拦截这个类的方法_类拦截器和方法拦截器

    一.拦截器 1.拦截器小介 拦截器的功能类似于web.xml文件中的Filter,能对用户的请求进行拦截,通过拦截用户的请求来实现对页面的控制.拦截器是在Struts-core-2.2.3.jar中进 ...

  7. Struts2配置拦截器

    配置拦截器有三个步骤: 第一:先创建一个拦截器的包 com.bdqn.interceptor package com.bdqn.interceptor;import com.opensymphony. ...

  8. 拦截器HandlerInterceptor+方法参数解析器HandlerMethodArgumentResolver用于统一获取当前登录用户信息

    文章目录 前言 一.拦截器+方法参数解析器 是什么? 二.具体实现步骤 1.自定义权限拦截器LoginInterceptor拦截所有request请求,并将token解析为currentUser,最终 ...

  9. Struts2 自定义拦截器(方法拦截器)

    转自:http://05061107cm.iteye.com/blog/365504 struts2系统自带了很多拦截器,有时需要我们自己定义,一般有两种方式: 一.实现Interceptor接口 J ...

最新文章

  1. linux0.11内核编译,编译Linux-0.11内核
  2. [codevs 1343] 蚱蜢(省队选拔赛湖南)
  3. Linux 自动删除N天前的文件
  4. Commonjs规范及Node模块实现
  5. Flink JDBC Connector:Flink 与数据库集成最佳实践
  6. 补习系列(5)-springboot- restful应用
  7. Arthas 实战,助你解决同名类依赖冲突问题
  8. Linked List Cycle给定一个链表,判断其中是否有环。
  9. php项目推荐,php教程推荐:最值得推荐的10个php教程
  10. linux pthread头文件,pthread t 头文件_uint8 t 头文件_pthread t 头文件
  11. 高含盐废水处理资源化——双极膜电渗析
  12. django里template中的书名号
  13. Python手写强化学习Q-learning算法玩井字棋
  14. JZOJ5912 Van♂Usee
  15. java socket ascii_TCP套接字上的ASCII - java
  16. PHP脚本中的两种换行符的区别
  17. css3图片倾斜3D效果
  18. PHP获取客户端和服务器端IP
  19. 【AtCoder】【思维】【图论】Splatter Painting(AGC012)
  20. 四、Scala从入门到精通一一循环控制

热门文章

  1. 认清企业现状,做好信息化建设
  2. java学习笔记(四)----对象、数组作为参数传递,静态变量、静态方法的使用,内部类,使用文档注释
  3. 【Linux 内核 内存管理】Linux 内核内存布局 ④ ( ARM64 架构体系内存分布 | 内核启动源码 start_kernel | 内存初始化 mm_init | mem_init )
  4. 【错误记录】Flutter 混合开发报错 ( Android 端与 Flutter 端 EventChannel 初始化顺序错误导致无法通信 | EventChannel 通信流程 )
  5. 【Android 异步操作】Handler ( 主线程中的 Handler 与 Looper | Handler 原理简介 )
  6. 【Netty】NIO 缓冲区 ( Buffer ) 分散 Scattering 与 聚合 Gathering 操作
  7. 【C++ 语言】异常 ( 抛出字符串异常 | 抛出异常对象 | 抛出任意对象 | 抛出自定义异常 )
  8. Vue状态管理之Vuex
  9. Charles抓包https
  10. 【BZOJ4764】弹飞大爷 LCT