springMvc-08 拦截器和异常处理
1、拦截器
拦截器是基于Java反射机制一级面向切面AOP技术的实现。
在SpringMvc中定义拦截器非常简单
第一种:通过实现HandlerInterceptor接口
第二种:继承HandlerInterceptor
1.1、代码例子
例子:检测用户是否登录
package rui.tool;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class checkLogin implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
System.out.println("First pre");
HttpSession session = request.getSession();
boolean isCheckLogin=true;
//判断用户登录
if(isCheckLogin) {
Object isLogin = session.getAttribute("isLogin");
if (isLogin == null) {
response.sendRedirect("/user/login");
return false;
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
System.out.println("post pre");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
System.out.println("after pre");
}
}
1.2、配置springMvc.xml
<!--配置拦截器-->
<mvc:interceptors>
<!--全局拦截器-->
<!--<bean class="rui.tool.checkLogin" />-->
<mvc:interceptor>
<!--根据配置先后顺序执行-->
<mvc:mapping path="/admin/**" />
<mvc:mapping path="/clientCenter/**"/>
<mvc:exclude-mapping path="/user/login" />
<mvc:exclude-mapping path="/**.jpg"/>
<mvc:exclude-mapping path="/**.gif"/>
<bean class="rui.tool.checkLogin" />
</mvc:interceptor>
</mvc:interceptors>
这里的path和exclude-path可以配置多个。
可以配置多个拦截器,多个的执行顺序类似套娃。
First-Pre,Second-Pre,Handle,SecondPost,FirstPost,render,SecondAfter,firstAfter.
2、异常处理
在程序开发过程中,不同层的代码都有可能产生异常,如何对未处理的异常进行统一处理,SpringMvc提供了3种处理异常的方式
1)简单异常处理器
SimpleMappingExceptionResolver,需要在SpringMvc配置文件中进行如下的配置:
<!--简单异常处理器-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--定义默认的异常处理界面-->
<property name="defaultErrorView>" value="/error" />
<property name="exceptionMappings">
<props>
<!--产生未处理的SqlException时,显示error-sql的页面-->
<prop key="java.sql.SQLException">/error-sql</prop>
<!--产生未处理的IOException时,显示error-io的页面-->
<prop key="java.io.IOException">/error-io</prop>
</props>
</property>
</bean>
2)实现异常处理接口
定义异常处理类
package rui.tool;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@Component
public class ExceptionHandle implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object o, Exception e) {
Map<String,Object> model = new HashMap<String,Object>();
/*保存异常*/
model.put("exception",e);
//根据异常类型,返回给不同的显示页面,并携带异常对象
if(e instanceof SQLException)
return new ModelAndView("/error-sql",model);
else if(e instanceof IOException)
return new ModelAndView("/error-io",model);
else
return new ModelAndView("/error",model);
}
}
springMvc中注入
<!--异常处理配置-->
<bean class="rui.tool.ExceptionHandle"></bean>
3)通过注解使用
这种方式无需在SpringMvc中进行注入
定义控制器基类,并增加注解
package rui.tool;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
public class baseController {
@ExceptionHandler
public String exception(HttpServletRequest request, HttpServletResponse response,Exception ex)
{
request.setAttribute("exception",ex);
//根据异常类型,返回给不同的显示页面,并携带异常对象
if(ex instanceof SQLException)
return "/error-sql";
else if(ex instanceof IOException)
return "/error-io";
else
return "/error";
}
}
让其它控制器继承该baseController即可。
4)异常信息展示页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
<title>错误显示页面</title>
</head>
<body>
<h3>异常错误</h3>
<%= exception %>
<h3>错误信息</h3>
<h4><% exception.printStackTrace(response.getWriter()); %></h4>
</body>
</html>

浙公网安备 33010602011771号