struts2自定义拦截器

作者: rainumdo | 来源:发表于2017-08-16 10:06 被阅读24次

题目:使用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>

相关文章

  • struts2 - 创建包级别的拦截器

    struts2的拦截器是核心,下面是自定义的拦截器, 自定义的拦截器需要继承AbstractInterceptor

  • struts2 自定义拦截器

    一、为什么要自定义拦截器 在struts2里面有很多拦截器,这些拦截器是struts2封装的功能,但是在实际开发中...

  • Struts2拦截器登录验证

    Struts2拦截器 Struts2拦截器的概念和Spring Mvc拦截器一样。 Struts2拦截器是在访问某...

  • Struts2拦截器

    自定义拦截器: 从struts2的apps中拷贝相应jar包 然后建struts2项目 加入到buildpath ...

  • struts2 拦截器介绍

    1.Interceptor介绍 拦截器(Intercepter):拦截器是struts2的核心,struts2的众...

  • struts2实验5:struts2 拦截器

    layout: post title: struts2实验5:struts2 拦截器 categories: S...

  • struts2自定义拦截器

    题目:使用struts2自定义拦截器,完成用户登陆才能访问权限的实现 在session中存放user变量表示用户登...

  • Struts2学习笔记(第三天)

    国际化 拦截器(interceptor) struts2中怎样使用拦截器 分析拦截器原理 关于intercepto...

  • 拦截器

    拦截器的概述: 1、struts2是框架,封装了很多的功能,struts2里面封装的功能都是在拦截器里面。2、st...

  • SSH框架之Struts2的拦截器(四)

    第一节:Struts2的拦截器 1.1 拦截器的概述: 拦截器,在AOP(Aspect - Oriented Pr...

网友评论

    本文标题:struts2自定义拦截器

    本文链接:https://www.haomeiwen.com/subject/nqmhrxtx.html