异常处理的两种方式 #.使用SpringMVC提供的异常处理器SimpleMappingExceptionResolver #.使用Spring的异常处理接口HandlerExceptionResolver自定义自己的异常处理机制 我们先看一下不使用异常处理器的情
异常处理的两种方式
#.使用SpringMVC提供的异常处理器SimpleMappingExceptionResolver
#.使用Spring的异常处理接口HandlerExceptionResolver自定义自己的异常处理机制
我们先看一下不使用异常处理器的情况
1.在service层模拟异常情况(接口代码省略)
public void show1() {
System.out.println("抛出类型转换异常....");
Object str = "zhangsan";
Integer num = (Integer)str;
}
public void show2() {
System.out.println("抛出除零异常....");
int i = 1/0;
}
public void show3() throws FileNotFoundException {
System.out.println("文件找不到异常....");
InputStream in = new FileInputStream("C:/xxx/xxx/xxx.txt");
}
public void show4() {
System.out.println("空指针异常.....");
String str = null;
str.length();
}
public void show5() throws MyException {
System.out.println("自定义异常....");
throw new MyException();
}
}
2.编写Controller层
@Controllerpublic class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping(value = "/show")
public String show() throws FileNotFoundException, MyException {
System.out.println("show running......");
//demoService.show1();
//demoService.show2();
//demoService.show3();
//demoService.show4();
demoService.show5();
return "index";
}
}
3.配置applicaitonContext.xml文件
<?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"
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">
<!--将service注入到spring容器-->
<bean id="demoService" class="com.hao.service.DemoServiceImpl"></bean>
</beans>
4.配置spring-mvc.xml文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--1、mvc注解驱动-->
<mvc:annotation-driven/>
<!--2、配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--3、静态资源权限开放-->
<mvc:default-servlet-handler/>
<!--4、组件扫描 扫描Controller-->
<context:component-scan base-package="com.hao.controller"/>
</beans>
5.启动tomcat访问
我们不可能让用户访问时看到这种页面一般的做法就是当某个程序出现异常时,让它跳转到某个jsp页面
使用SpringMVC提供的异常处理器SimpleMappingExceptionResolver
6.所以我们可以在spring-mvc.xml中加入这一段代码
defaultEroorView:当后面map集合里面的异常都不匹配时,才会执行这个默认的配置
value:代表视图,不写error.jsp是因为我在上文中已经配置了视图解析器
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
<property name="exceptionMappings">
<map>
<entry key="java.lang.ClassCastException" value="error1"/>
<entry key="com.hao.exception.MyException" value="error2"/>
</map>
</property>
</bean>
7.再次启动tomcat进行访问(出现异常就会帮我们跳转到错误页面)
自定义异常处理器
编写异常处理类
public class MyExceptionResolver implements HandlerExceptionResolver {/*
参数Exception:异常对象
返回值ModelAndView:跳转到错误视图信息
*/
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
if(e instanceof MyException){
modelAndView.addObject("info","自定义异常");
}else if(e instanceof ClassCastException){
modelAndView.addObject("info","类转换异常");
}
modelAndView.setViewName("error");
return modelAndView;
}
}
2.将自定义异常处理类配置到spring-mvc.xml文件中
<bean class="com.hao.resolver.MyExceptionResolver"/>