【框架】过滤器filter、拦截器Interceptor、监听器listener

目录

一、过滤器Filter:

 1、实例: 

2、自定义拦截器类,继承servlet包下的Filter接口

二、拦截器:Interceptor:

实例:

三、监听器listener:

实例:


一、过滤器Filter:

        类似于AOP面向切面编程;提供了一种声明式的服务,有可插拔能力;Filter对Request,Response请求进行了拦截,在初始化的时候被实例化一次,之后不会再被实例化

 1、实例: 

比如:所有请求的页面设置统一的字符集:

2、自定义拦截器类,继承servlet包下的Filter接口

当有请求是url带有.jsp结尾,调用filter-name=CharseEncodingFilter的拦截器类

web.xml下配置拦截信息

1、filter拦截器是谁fiter-name;      路径是啥 filter-mapping

2、fiter-mapping中的url-pattern要对谁拦截

<filter>

  <filter-name>CharseEncodingFilter</filter-name>

  <filter-class>filter.CharseEncodingFilter</filter-class>

</filter>

<filter-mapping>

  <filter-name>CharseEncodingFilter</filter-name>

  <url-pattern>*.jsp</url-pattern>

</filter-mapping>

 

import javax.servlet.*;
import java.io.IOException;

/**

* Created by 邢美玲 on 2020/3/10 18:27.

* 字符集类型设置的拦截方法

* @Version 1.0

*/

public class CharseEncodingFilter implements Filter {

    @Override

    public void init(FilterConfig filterConfig) throws ServletException {

    }


    @Override

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        // 设置字符集类型

        servletRequest.setCharacterEncoding("UTF-8");

        // 继续执行后面的内容

        filterChain.doFilter(servletRequest,servletResponse);

    }


    @Override

    public void destroy() {

    }

}

 

二、拦截器:Interceptor:

  拦截器(Interceptor):是Struts2框架的核心功能之一,Struts 2是一个基于MVC设计模式的开源框架, [3]  主要完成请求参数的解析、将页面表单参数赋给值栈中相应属性、执行功能检验、程序异常调试等工作。

Struts2拦截器是一种可插拔策略,实现了面向切面的组件开发,当需要扩展功能时,只需要提供对应拦截器,并将它配置在Struts2容器中即可,如果不需要该功能时,也只需要在配置文件取消该拦截器的设置,整个过程不需要用户添加额外的代码。

拦截器中更为重要的概念即拦截器栈(Interceptor Stack),拦截器栈就是Struts2中的拦截器按一定的顺序组成的一个线性链,页面发出请求,访问Action对象或方法时,栈中被设置好的拦截器就会根据堆栈的原理顺序的被调用。

实例:

自定义拦截器:截获用户请求, 判断用户是否已经登录

        拦截类继承MethodFilterInterceptor 或者 AbstractInterceptor(MethodFilterInterceptor 是 AbstractInterceptor的子类 )

Struts下的xml文件中配置

在用于请求该addminFirst_findAll.action的时候调用name=PrivilegeInterceptor的拦截类,起到在执行方法之前先判断权限的目的

<!--后端管理端的权限拦截器-->

<interceptors>

    <interceptor name="PrivilegeInterceptor" class="interceptor.PrivilegeInterceptor"/>

</interceptors>

。。。。。。
<!--管理端一级分类管理Action-->

<action name="adminFirst_*" class="adminCategoryAction" method="findAll">

    <result name="findAllSuccess">/admin/category/list.jsp</result>

    <interceptor-ref name="PrivilegeInterceptor"/>

    <interceptor-ref name="defaultStack"/>

</action>

 

从ServletActionContext的Session中获取到值

public class PrivilegeInterceptor extends MethodFilterInterceptor{



   @Override

   protected String doIntercept(ActionInvocation actionInvocation) throws Exception {

      // 判断是否登录,如果登录,放行,没有登录,跳转到登录页面.

      AdminuserEntity adminUser = (AdminuserEntity) ServletActionContext.getRequest()

            .getSession().getAttribute("existAdminUser");

      if(adminUser != null){

         // 已经登录过,继续向下调用

         return actionInvocation.invoke();

      }else{

         // 跳转到登录页面:

         ActionSupport support = (ActionSupport) actionInvocation.getAction();

         support.addActionError("您还没有登录!没有权限访问!");

         return "loginFail";

      }

   }

}


三、监听器listener:

在Servlet规范中定义了多种类型的监听器,它们用于监听的事件源分别为 ServletContext, HttpSession 和 ServletRequest 这三个域对象。

实例:

需求描述:登录的时候 每登录一次就需要加载一次权限信息,所以在用户第一次启动运行项目的时候,把所有权限的功能都加载存放在Application中

自定义监听器 继承servlet-api下的ServletContextListener

public class InitListener implements ServletContextListener  {

    /*初始化的时候*/

    @Override

    public void contextInitialized(ServletContextEvent sce) {

        System.out.println("加载------contextInitialized");

        // 获取容器与相关的Service对象

        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());

        PrivilegeService privilegeService = (PrivilegeService) ac.getBean("privilegeServiceImpl");


        // 把所有权限都加载存入Application

        List<Privilege> privilege = privilegeService.findAll();

        sce.getServletContext().setAttribute("privilegeList",privilege);

    }

}

【对代码中的如下部分说明】

ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());

        PrivilegeService privilegeService = (PrivilegeService) ac.getBean("privilegeServiceImpl");

个人理解:在项目初始化加载的时候调用这个监听器类,这个类是通过配置文件web.xml读取,通过反射机制的实现的实例化,

这里的例子是集成了spring,所以想要获取Service层的业务类,需要获取到spring的容器,从容器中获取到你需要的对象。

spring提供了一个这样的接口WebApplicationContextUtils,可以获取到你需要的对象进行调用

 

前端JSP页获取方式:#application.privilegeList

    <%-- 显示一级菜单 --%>

            <s:iterator value="#application.privilegeList">

               <li class="level1">

                  <div onClick="menuClick(this);" class="level1Style">

                     <img src="style/images/MenuIcon/${id}.gif" class="Icon" />

                     ${name}

                  </div>

               </li>

            </s:iterator>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邢美玲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值