我们上次大致说完了执行流程,也只是说了大致的过程,还有中间会出错的情况我们来处理一下。
统一异常处理
比如我们的运行时异常的500错误。我们来自定义一个类
package com.springmvcbk.utils; 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.ByteArrayOutputStream; import java.io.PrintStream; public class MyExceptionResolver implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView mv = new ModelAndView(); mv.setViewName("/WEB-INF/page/error.jsp"); if (ex instanceof IllegalAccessException){ mv.addObject("erroeType","非法参数"); }else{ mv.addObject("erroeType",ex.getClass().getSimpleName()); } mv.addObject("message",ex.getMessage()); ByteArrayOutputStream bo = new ByteArrayOutputStream(); ex.printStackTrace(new PrintStream(bo,true)); mv.addObject("stack",bo.toString()); return mv; } }
将我们的类注入到我们的spring-mvc.xml内
<bean class="com.springmvcbk.utils.MyExceptionResolver"/>
我们让我们的代码运行时异常就可以跳转到我们的error页面上去了。
拦截器
还有我们常用的拦截器,也就是我们拿到的HandlerInterceptor来处理拦截的。
package com.springmvcbk.utils; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyInterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle"); return true; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion"); } }
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.springmvcbk.utils.MyInterceptor"/> </mvc:interceptor> </mvc:interceptors>
配置我们的拦截范围也就可以了。如果执行过程出现错误就不会执行postHandle了。
还有最后一项,我们最熟悉的@RequestMapping注解的形式,我们还是先来一个最精简的启动
package com.springmvcbk.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Controller public class SpringmvController { @RequestMapping(value = "zhangsan") public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/page/index.jsp"); modelAndView.addObject("name","张三"); return modelAndView; } }
<?xml version="1.0" encoding="GBK"?> <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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.springmvcbk.controller"/> <mvc:annotation-driven/> </beans>
还有我们上次忘记的一个配置,每次返回都要写全路径,累死了,我们在spring-mvc.xml加入这样的配置
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="WEB-INF/page/"/> <property name="suffix" value=".jsp"/> <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/> </bean>
加入这样的配置就可以了,完事,配置就是这样的简单,和配置文件不同的是加入一个扫描器就可以了。与上次的流程几乎是一致的,映射器和适配器有一点点的差异。
今天我们就说到这里了, 关于SpringMVC后面我会尽快给出一个完整的面试题。
最进弄了一个公众号,小菜技术,欢迎大家的加入