拦截器

##什么是拦截器

spring提供的一个特殊的组件,当DispatcherServlet收到请求后,如果有拦截器,会先调用拦截器,然后调用相应的处理器(Controler)。

注:过滤器属于servlet规范,而拦截器属于spring框架。

##如何写一个拦截器

  1.写一个Java类,实现HandlerInterceptor接口。

  2.实现具体的拦截处理逻辑,比如,session验证。

  3.配置拦截器。

package com.outlook.menbozg.interceptors;

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

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class SessionInterceptor implements HandlerInterceptor{

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("preHandler()");
        HttpSession session = request.getSession();
        Object object = session.getAttribute("admin");
        if(object == null) {
            //没有登陆,重定向到登陆页面
            response.sendRedirect("toLogin.form");
            return false;
        }
        //已经登陆过,则允许访问。
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- 配置组件扫描 -->
    <context:component-scan base-package="com.outlook.menbozg"></context:component-scan>
    <!-- 配置mvc注解扫描 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <util:properties id="jdbc" location="classpath:db.properties" /> 
    <!-- 配置连接池 -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="#{jdbc.driverclass}" />      
          <property name="url" value="#{jdbc.url}" />      
          <property name="username" value="#{jdbc.user}" />      
          <property name="password" value="#{jdbc.password}" />  
           <property name="maxActive" value="#{jdbc.maxActive}"/>
    </bean>
    <!-- 配置拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="/toLogin.form"/>
            <mvc:exclude-mapping path="/login.form"/>
            <bean class="com.outlook.menbozg.interceptors.SessionInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    
</beans>

## spring异常处理

可以将异常抛给spring,由spring来处理这些异常

具体有两个方法:

## 使用spring简单异常处理器

QQ截图20190908175456

##注解

@ExceptionHandler

  1. 在处理器类中,添加一个异常处理方法,该方法必须使用@ExceptionHandler修饰。

注:在该方法里面,依据异常类型,分别进行不同的处理。

  2.添加异常处理页面。

QQ截图20190908180837QQ截图20190908180714

原文地址:https://www.cnblogs.com/menbozg/p/11486521.html