过滤器***

基于Spring的过滤器:

作用:对web请求包括请求参数做过滤操作
缺点:一个过滤器实例只能在容器初始化时调用一次
特点:依赖于Servlet容器,随着Web容器启动而启动,关闭而销毁

配置文件:

web.xml
<!-- 过滤器拦截指定后缀-->	

  	<!-- //localhost:8080/Ycat/test/****都会被拦截 -->

        <!--配置过滤器-->
	<filter>
		 <filter-name>SessionFilter</filter-name>
                    
                  <!--指定过滤用的是哪个过滤器-->
		 <filter-class>com.Ycat.login.filter.MyFilter</filter-class>

                  <!--避免乱码-->
		 <init-param>   
	          <param-name>charset</param-name>   
          	  <param-value>UTF-8</param-value>   
	     </init-param>   
                    <init-param>   
	          <param-name>contentType</param-name>   
	          <param-value>text/html;charset=UTF-8</param-value>   
	      </init-param>     <init-param>
   <!--指定不过滤的url -->    <param-name>excludeUrl</param-name>  <param-value>/login</param-value>  </init-param>  <init-param>  <!--指定被过滤后的请求 跳转过去的url -->  <param-name>dispatchUrl</param-name>  <param-value>/login.jsp</param-value>  </init-param>

   
	</filter>

        <!--配置过滤器映射信息-->
	<filter-mapping>
                   <!--用的是哪一个过滤器-->
		 <filter-name>SessionFilter</filter-name>
		 <!-- 要过滤的哪一级目录 -->
		 <url-pattern>/test/*</url-pattern>
	</filter-mapping>

	<!-- 配置session超时时间,单位分钟 -->
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
  • 过滤什么请求
  • 不过滤什么请求
  • 过滤哪一级目录

编写过滤类:servlet的Filter

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyFilter implements Filter{

	//初始化过滤器的时候会执行的方法
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	//过滤请求的时候执行的方法
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		
	}

	//web应用关闭要销毁过滤器时候执行的方法
	public void destroy() {
		// TODO Auto-generated method stub
		
	}


}


基于SpringMVC的***:


spring-web.xml

    <!-- 配置*** -->
	<mvc:interceptors>
		<mvc:interceptor>
			<!-- 指定拦截路径 -->
			<mvc:mapping path="/**"/>

                        <!-- 指定放行路径 -->
			<mvc:exclude-mapping path="XXX" />   <!--指定***-->  <bean class="XX.XX"></bean>			
		</mvc:interceptor>
	</mvc:interceptors>

编写拦截类

继承HandleInterceptor接口 选择性重写三个拦截方法
  • preHandle:请求到达Controller之前执行,如果执行结果为F拦截后跳转到拦截页面,执行结果为T继续执行
  • postHandle:在Controller执行结束后,在视图渲染前执行,执行结果为T继续执行
  • afterCompletion:在视图渲染结束后执行

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.db.common.exception.ServiceException;
/**
 * 编写***对象,对控制层层登陆方法进行拦截
 * @author Administrator
 *说明:***编写好以后,需要对***进行配置
 */
@Component//交给spring管理
public class HandlerAccessInterceptor extends HandlerInterceptorAdapter {

	@Override( )
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// 执行完毕,返回前拦截		
	}

	@Override( )
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// 在处理过程中,执行拦截		
	}

	@Override( )
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// 在拦截点执行前拦截,如果返回true则不执行拦截点后的操作(拦截成功)
		业务处理
		if(业务判断) {
			
			//不拦截
			return true;
		
		}else {
			// 拦截后跳转页面
			response.sendRedirect(request.getContextPath()+"XXX");
			return false;
		}

	}

}

	
}

三个拦截方法执行顺序:




***:

做完某一件事情以后,需要广播一些消息或者通知,告诉其他的模块进行一些事件处理,事件监听也是设计模式中 发布-订阅模式、观察者模式的一种实现。

角色分配:

  • 事件:***监听目标,可以理解为一种行为
  • ***:事件发生后的业务处理模块
  • 事件发布者:触发事件

非注解模式下的***:

事件:继承ApplicationEvent

package com.mu.event;

 
import org.springframework.context.ApplicationEvent;

public class MyTestEvent extends ApplicationEvent{

    private static final long serialVersionUID = 1L; 

    private String msg ; 

    public MyTestEvent(Object source,String msg) {
        super(source);
        this.msg = msg;
    } 

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

***:

  • 在需要实现ApplicationListener,并且需要传入事件的泛型参数
  • 在***中添加业务处理,也就是监听到事件发生后***该做的事情
package com.mu.listener;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import com.mu.event.MyTestEvent;

@Component( )
public class MyNoAnnotationListener implements ApplicationListener<MyTestEvent>{
    @Override( )
    public void onApplicationEvent(MyTestEvent event) {
        System.out.println("非注解***:" + event.getMsg());
    }

}

事件发布者:

  • 用于触发事件的发生,导致***监听到事件发生后,做出响应
  • 事件发布的方法就算是通过applicationContext来调用publishEvent,参数就是要发布的目标事件的对象
package com.mu.event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component( )
public class MyTestEventPubLisher {
    @Autowired
    private ApplicationContext applicationContext;

    // 事件发布方法
    public void pushListener(String msg) {
        applicationContext.publishEvent(new MyTestEvent(this, msg));
    }

}

测试类:

package com.mu.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mu.event.MyTestEventPubLisher;

@Controller
public class TestEventListenerController {

    @Autowired
    private MyTestEventPubLisher publisher;
    @RequestMapping(value = "/test/testPublishEvent1" )
    public void testPublishEvent(){
        publisher.pushListener("我来了!");
    }
}

注解模式下的***:

  • 还是需要Event角色
  • 不需要publiser
  • 在***上添加@EventListener注解
package com.mu.listener;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import com.mu.event.MyTestEvent;

 
@Component( )
public class MyAnnotationListener {
    @EventListener
    public void listener1(MyTestEvent event) {
        System.out.println("注解***1:" + event.getMsg());
    }

}
  • 如果想要使得该***的业务是异步执行的,可以在@EventListener之后添加@Async注解


配置SpringMvc中的ContextLoaderListener***

在启动Web 容器时,自动装配 Spring applicationContext.xml 的配置信息。

xml配置文件:

</pre><pre class="java" name="code">
<!--指定spring核心配置文件的位置-->
<context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:applicationContext-*.xml</param-value>
 </context-param>

<!--指定spring使用的***-->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>





全部评论
由于牛客网博客敏感词原因,拦截器和***无法显示 第二部分为拦截器 最后部分为***
点赞 回复 分享
发布于 2019-10-06 00:43

相关推荐

05-01 22:41
中南大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务