• 分享知识-快乐自己:Struts2 拦截器 与 过滤器


    拦截器的使用以及配置:

    package com.gdbd.interceptor;
    
    import com.gdbd.pojo.UserInfo;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;
    
    import java.util.Map;
    
    /**
     * 登陆拦截器:(拦截的只是 Action 请求路径)
     * (可根据自行情况进行更改)
     * @author asus
     */
    public class LoginInterceptor implements Interceptor {
        @Override
        public void destroy() {
            System.out.println("销毁");
        }
    
        @Override
        public void init() {
            System.out.println("初始化");
        }
    
        @Override
        public String intercept(ActionInvocation invocation) throws Exception {
            System.out.println("---------------拦截器-------------------");
            String actionName = invocation.getProxy().getActionName();
            Map<String, Object> session = invocation.getInvocationContext().getSession();
            UserInfo userInfo = (UserInfo) session.get("userInfo");
            if ("login".equals(actionName)) {
                String invoke = invocation.invoke();
                return invoke;
            }
            if (userInfo != null) {
                String invoke = invocation.invoke();
                return invoke;
            }
            return "login";
        }
    }

    struts.xml:关键配置

    <!--
       定义拦截器
    -->
    <interceptors>
        <!--自定义拦截器-->
        <interceptor name="loginInter" class="com.gdbd.interceptor.LoginInterceptor"/>
        <!--拦截器栈-->
        <interceptor-stack name="MyStack">
           <interceptor-ref name="defaultStack"/>
           <interceptor-ref name="loginInter"/>
        </interceptor-stack>
    </interceptors>
    <!--全局范围拦截定义-->
    <default-interceptor-ref name="MyStack"/>

    或者...指定 Action
    <action name="login" class="userLoginAction" method="password">
    <result name="login">/login.jsp</result>
    <result name="main" type="redirect">/main.jsp</result>
    <interceptor-ref name="MyStack"/>
    </action>

    过滤器的使用以及配置:

    package com.gdbd.filter;
    import com.gdbd.pojo.UserInfo;
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    /**
     * 原理是,将所有的地址中包含JSP的访问拦截,将访问重定位到网站的跟目录
     * (根据自行情况可进行更改)
     * @author asus
     */
    public class URLFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("Filter 初始化");
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
                throws IOException, ServletException {
            System.out.println("---------------过滤器-------------------");
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            //获取请求的URL路径
            StringBuffer url = httpServletRequest.getRequestURL();
            HttpSession session = httpServletRequest.getSession();
            UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
            if (userInfo != null) {
                filterChain.doFilter(request, response);
                return;
            }
            //判断地址中是否包含"JSP"
            if (url.indexOf("login.jsp") > 0) {
                filterChain.doFilter(request, response);
            } else if (url.indexOf("jsp") > 0) {
                HttpServletResponse httpres = (HttpServletResponse) response;
                //跳转到网站根目录,也可以根据自己的需要重定位到自己的Action
                httpres.sendRedirect("/login.jsp");
                return;
            } else { //不包含JSP,则继续执行
                filterChain.doFilter(request, response);
            }
        }
        @Override
        public void destroy() {
            System.out.println("Filter 销毁");
        }
    }

    WEB.XML 配置

        <!--过滤请求的URL路径-->
        <filter>
            <filter-name>URLfilter</filter-name>
            <filter-class>com.gdbd.filter.URLFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>URLfilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    提示:

    楼主A:在进行SSH整合的时候使用到 拦截器 与 过滤器 (基本配置);发现 会先走 拦截器 然后再走 过滤器  。

     

  • 相关阅读:
    【穿插】Python基础之文件、文件夹的创建,对上一期代码进行优化
    爬虫实战【5】送福利!Python获取妹子图上的内容
    爬虫实战【4】Python获取猫眼电影最受期待榜的50部电影
    爬虫实战【3】Python-如何将html转化为pdf(PdfKit)
    爬虫实战【2】Python博客园-获取某个博主所有文章的URL列表
    爬虫实战【1】使用python爬取博客园的某一篇文章
    Oracle中Lpad函数和Rpad函数的用法
    oracle中如何判断blob类型字段是否为空
    将图片文件转化为字节数组字符串,并对其进行Base64编码处理,以及对字节数组字符串进行Base64解码并生成图片
    oracle中使用函数控制过程是否执行(结合job使用)
  • 原文地址:https://www.cnblogs.com/mlq2017/p/10020932.html
Copyright © 2020-2023  润新知