美文网首页
spring拦截器

spring拦截器

作者: 万物皆有序和公式 | 来源:发表于2019-05-06 13:52 被阅读0次
public class ExpectionProcessInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {


    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {
        if (e != null) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("resultCode", 1);
            if (e instanceof BusinessException) {
                jsonObject.put("resultInfo", e.getMessage());
            } else if (e instanceof Exception) {
                jsonObject.put("resultInfo", "系统异常");
            }
            e.printStackTrace();
            response.setContentType("application/json;charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            PrintWriter out = response.getWriter();
            out.print(jsonObject.toJSONString());
            out.flush();
            out.close();
        }
    }
}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:interceptors>
        <mvc:interceptor>
            <!-- 对所有的请求拦截使用/** ,对某个模块下的请求拦截使用:/myPath/* -->
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/**/*.html"/>
            <mvc:exclude-mapping path="/**/*.css"/>
            <mvc:exclude-mapping path="/**/*.js"/>
            <mvc:exclude-mapping path="/**/*.jpeg"/>
            <mvc:exclude-mapping path="/**/*.gif"/>
            <mvc:exclude-mapping path="/**/*.png"/>
            <mvc:exclude-mapping path="/**/*.eot"/>
            <mvc:exclude-mapping path="/**/*.otf"/>
            <mvc:exclude-mapping path="/**/*.svg"/>
            <ref bean="exceptionInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
    <bean name="exceptionInterceptor" class="com.ttf.gateway.web.ExpectionProcessInterceptor"/>
</beans>

相关文章

  • Spring知识点

    一、自定义拦截器 spring mvc拦截器和spring拦截器相比,它里面能够获取HttpServletRequ...

  • Spring15-拦截器

    定义拦截器 定义拦截器需要实现HandlerInterceptor 配置拦截器 注意:spring mvc的拦截器...

  • 【SpringMVC】拦截器与异常处理

    SpringMVC的拦截器 01-SpringMVC拦截器-拦截器的作用(理解) Spring MVC 的拦截器类...

  • SpringMVC的拦截器

    SpringMVC的拦截器 01-SpringMVC拦截器-拦截器的作用(理解) Spring MVC 的拦截器类...

  • 33 Spring AOP拦截器的序列

    Spring AOP 事务不是工作在以下拦截器? matchGenericTxInterceptor”事务拦截器,...

  • SpringMVC 的处理拦截器

    0 目录 1 Spring MVC拦截器流程图 2 Spring Web MVC 的处理器拦截器 类似于Servl...

  • HandlerInterceptorAdapter或Handle

    Spring拦截器 HandlerInterceptorAdapter需要继承,HandlerIntercepto...

  • Spring Boot - 拦截器

    在Spring Boot项目中使用Spring MVC拦截器仅需简单2步就能实现。 以下内容以登录拦截器为例,介绍...

  • Struts2拦截器登录验证

    Struts2拦截器 Struts2拦截器的概念和Spring Mvc拦截器一样。 Struts2拦截器是在访问某...

  • 第十五章 拦截器

    拦截器的定义 什么是拦截器(Interceptor) Spring MVC中的拦截器类似于servlet中的过滤器...

网友评论

      本文标题:spring拦截器

      本文链接:https://www.haomeiwen.com/subject/vimroqtx.html