zoukankan      html  css  js  c++  java
  • struts自定义拦截器

    第01步:配置web.xml,启动struts框架

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    第02步:编写action类

    package com.self.action;
    
    public class InterceptorAction {
        private String message ;
        
        public String login(){
            this.message="登录成功!";
            return "success";
        }
        
        public String addUser(){
            this.message="添加用户";
            return "success";
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }

    第03步:编写拦截器类

    package com.self.action;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;//导入包,包里含有Interceptor类
    
    /**第01 步:编写拦截器**/
    public class Permission implements Interceptor {
    
        public void destroy() {
            
        }
    
        public void init() {
            
        }
    
        public String intercept(ActionInvocation invocation) throws Exception {//属于aop编程的环绕通知
            String reString="";
            //01:判断用户是否登录,登录就有权限执行action方法,没登陆就没权限
            Object user=ActionContext.getContext().getSession().get("user");
            if(user!=null){
                reString=invocation.invoke();//执行被拦截的action方法,被拦截的action方法有返回的字符串,原样返回
                ActionContext.getContext().put("message", "允许执行该操作");
                System.out.println("执行拦截的方法,并返回字符串:"+reString);
                //return invocation.invoke();可以直接这样
            }else{
                reString="error";
                ActionContext.getContext().put("message", "不允许执行该操作!");
                System.out.println("执行拦截的方法,并返回字符串:"+reString);
            }
            return reString;//原样返回action方法的字符串
        }
    
    }

    第04步:配置struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
        <constant name="struts.action.extension" value="do,action"/>
        <!-- 第02步:配置action -->
        <package name="intercept" namespace="/" extends="struts-default">
        
            <!-- 第03步:注册拦截器 -->
            <interceptors>
                <!-- 03.1:自定义拦截器配置 -->
                <interceptor name="permission" class="com.self.action.Permission" />
                <!-- 03.2 :定义拦截器栈,不定义会失去很多struts自身的拦截器-->
                <interceptor-stack name="permissionStackss">
                    <!-- 03.4:引入系统拦截器栈:defaultStack,系统拦截器栈放前面,先执行 -->
                    <interceptor-ref name="defaultStack"/>
                    <!-- 03.5:引入自定义拦截器 -->
                    <interceptor-ref name="permission" />
                </interceptor-stack>
            </interceptors>
            
            <global-results>
                <result name="error">
                    /error.jsp
                </result>
            </global-results>
            <action name="list_*" class="com.self.action.InterceptorAction" method="{1}">
                <!-- 03.6:自定义拦截器先执行:"permissionStackss"还是"permissionStacks" -->
                <interceptor-ref name="permissionStackss" />
                <result name="success">
                    /show.jsp
                </result>
            </action>
        </package>
    </struts>

    第05步:编写界面,session有数据,拦截器通过

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <html>
    <head>
    </head>
    <body>
        <%
            request.getSession().setAttribute("user", "zouli");
        %>
        <center>
            <a href="list_login.action">用户登录action!</a><BR>
            <a href="list_addUser.action">添加用户action!</a><BR>
        </center>
        <br>
    </body>
    </html>

    第06步:编写界面,session没数据,拦截器不通过

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <html>
    <head>
    </head>
    <body>
        <%
            request.getSession().removeAttribute("user");
        %>
        <center>
            没有用户<BR>
            <a href="list_login.action">用户登录action!</a><BR>
            <a href="list_addUser.action">添加用户action!</a><BR>
        </center>
        <br>
    </body>
    </html>

    第07步:编写通过显示界面

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <html>
    <head>
    </head>
    <body>
        <center>
        登录信息:message:${message }
        </center>
        <br>
    </body>
    </html>

    第08步:编写不通过错误界面

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <html>
    <head>
    </head>
    <body>
        <center>
            拦截信息:message:${message }<BR>
            拦截器不能使用错误!
        </center>
    </body>
    </html>

    注意:需要导入包

  • 相关阅读:
    关于Maya Viewport 2.0 API 开发的介绍视频
    春节大假
    Some tips about the life cycle of Maya thread pool
    Can I compile and run Dx11Shader for Maya 2015 on my side?
    How to get current deformed vertex positions in MoBu?
    想加入全球首届的 欧特克云加速计划吗?
    三本毕业(非科班),四次阿里巴巴面试,终拿 offer(大厂面经)
    mac、window版编辑器 webstorm 2016... 永久破解方法。
    node 搭载本地代理,处理web本地开发跨域问题
    js 一维数组,转成嵌套数组
  • 原文地址:https://www.cnblogs.com/zjsy/p/4306369.html
Copyright © 2011-2022 走看看