struts2自定义拦截器

题目:使用struts2自定义拦截器,完成用户登陆才能访问权限的实现

  • 在session中存放user变量表示用户登陆,若user为空则用户没有登陆,反之登陆
  • 显示提示信息(请先登录)
  1. 定义拦截器
    在struts.xml中定义拦截器使用标签<Intercaptors>、<Intercapter>。
    <interceptors>
            <interceptor name="test" class="Intercaptor.Intercaptor" />
            <interceptor-stack name="testStack">
                <interceptor-ref name="defaultStack"/>
                <interceptor-ref name="test" />
            </interceptor-stack>
    </interceptors>

注:当我们为某个action添加Intercaptor时就会放弃struts2的其他的拦截器,所以我们要把自定义的拦截器放在一个一个拦截器栈中。

name属性就是Intercaptor.Intercaptor类在服务器上的一个实例
class属性就是这个拦截器的的类

  1. 实现拦截器
    拦截器的java类要实现Intercaptor这个接口和里面的方法intercept()。我们这里拦截的条件是用户是否登陆,也就是session中的user变量是否为空。
public class Intercaptor implements Interceptor{

    public void destroy() {
    }

    public void init() {

    }

    public String intercept(ActionInvocation invocation) throws Exception {
        Object user=ActionContext.getContext().getSession().get("user");
        if(user!=null){
            return  invocation.invoke();
        }
        ActionContext.getContext().put("message", "请先登陆");
        return "success";
    }
}
  1. 实现业务逻辑
  • 在action中添加拦截器
    <action name="Action" class="Action.Action">
            <interceptor-ref name="test"></interceptor-ref>
            <result name="success">Message.jsp</result>
    </action>
  1. 其他
  • action的实现
public class Action extends ActionSupport{
    private String message;
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String execute() throws Exception {
        return "success";
    }
}
  • index.jsp
  <body>
    用户状态:${user!=null?"已登陆":"未登陆"}<br>
    <a href="UserLogin.jsp">用户登陆</a>
    <a href="UserQuit.jsp">用户退出</a>
    <form action="<%request.getContextPath(); %>/testIntercaptor/Action">
        <input type="submit" value="登陆后的操作">
    </form>
  </body>
登陆页面
  • UserLogin.jsp
    在request.getSesssion中存放user变量
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

 登陆成功
   <%
    request.getSession().setAttribute("user", "user");
    response.setHeader("refresh", "1;url=index.jsp");
   %>
  • UserQuit.jsp
    移除request.getSesssion中user变量
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

 退出成功
   <%
    request.getSession().removeAttribute("user");
        response.setHeader("refresh", "1;url=index.jsp");
   %>
  • Message.jsp
    简单是输出message和debug
  <body>
    ${message } <br/>
  <s:debug></s:debug>
  </body>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 本文简单介绍如何自定义一个Struts拦截器,但不涉及拦截器基础、原理等其他知识,仅仅只是介绍自定义拦截器的...
    小山雀阅读 197评论 0 0
  • 详谈 Struts2 的核心概念 本文将深入探讨Struts2 的核心概念,首先介绍的是Struts2 的体系结构...
    可爱傻妞是我的爱阅读 1,158评论 0 2
  • 概述 什么是Struts2的框架Struts2是Struts1的下一代产品,是在 struts1和WebWork的...
    inke阅读 2,276评论 0 50
  • 1.自定义拦截器 创建拦截器方式三种 练习,拦截除login方法之外所有方法 判断是否是window框架本身 in...
    路人爱早茶阅读 209评论 0 0
  • 那只猫死了,琥珀的眼,斑驳如虎的纹。它安静地趴在水泥路面上,背对着它曾无数次嬉戏过的田野。 它曾见证过男孩...
    蓦墨漠阅读 96评论 0 0