分享知识-快乐自己: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整合的时候使用到 拦截器 与 过滤器 (基本配置);发现 会先走 拦截器 然后再走 过滤器  。

 

猜你喜欢

转载自www.cnblogs.com/mlq2017/p/10020932.html