浅谈springMVC拦截器和过滤器总结

网友投稿 198 2023-06-17


浅谈springMVC拦截器和过滤器总结

拦截器: 用来对访问的url进行拦截处理

用处: 权限验证,乱码设置等

spring-mvc.xml文件中的配置:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://springframework.org/schema/tx

">

对所有的url进行拦截

java代码

package com.hbut.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* @Author XiJun.Gong

* @DATE 2016/6/1.

* aim: com.hbut.interceptor

*/

public class TestInterceptor implements HandlerInterceptor {

@Override

public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

//todo 在此处添加要操作code

System.out.println("preHandle");

return true; //todo 此处为false时,请求不会到达control层

}

@Override

public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

System.out.println("postHandlenTnNdEI"); //todo 可以用来修改信息,跳转等

}

@Override

public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

System.out.println("afterCompletion"); //todo 最后执行

}

}

另一种拦截器:大同小异

package com.hbut.interceptor;

import org.springframework.ui.ModelMap;

import org.springframework.web.context.request.WebRequest;

import org.springframework.web.context.request.WebRequestInterceptor;

/**

* @Author XiJun.Gong

* @DATE 2016/6/1.

* aim: com.hbut.interceptor

*/

public class Test2Interceptor implements WebRequestInterceptor {

http:// @Override

public void preHandle(WebRequest webRequest) throws Exception {

}

@Override

public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {

}

@Override

public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {

}

}

过滤器: 依赖于servlet容器,使用回调函数,过滤范围大

拦截器: 依赖于框架容器 比如spring、mybatis ,灵活

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://springframework.org/schema/tx

">

对所有的url进行拦截

java代码

package com.hbut.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* @Author XiJun.Gong

* @DATE 2016/6/1.

* aim: com.hbut.interceptor

*/

public class TestInterceptor implements HandlerInterceptor {

@Override

public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

//todo 在此处添加要操作code

System.out.println("preHandle");

return true; //todo 此处为false时,请求不会到达control层

}

@Override

public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

System.out.println("postHandlenTnNdEI"); //todo 可以用来修改信息,跳转等

}

@Override

public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

System.out.println("afterCompletion"); //todo 最后执行

}

}

另一种拦截器:大同小异

package com.hbut.interceptor;

import org.springframework.ui.ModelMap;

import org.springframework.web.context.request.WebRequest;

import org.springframework.web.context.request.WebRequestInterceptor;

/**

* @Author XiJun.Gong

* @DATE 2016/6/1.

* aim: com.hbut.interceptor

*/

public class Test2Interceptor implements WebRequestInterceptor {

http:// @Override

public void preHandle(WebRequest webRequest) throws Exception {

}

@Override

public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {

}

@Override

public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {

}

}

过滤器: 依赖于servlet容器,使用回调函数,过滤范围大

拦截器: 依赖于框架容器 比如spring、mybatis ,灵活


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:bootstrap输入框组件使用方法详解
下一篇:Spring boot实现数据库读写分离的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~