异常处理
1.在SpringMVC配置文件(springMVC.xml)中做以下配置:
<!-- 异常处理 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.NullPointerException">error</prop>
</props>
</property>
</bean>
2.index.jsp
<a href="testException">测试异常</a>
3.TestExceptionController.java
package com.atguigu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class TestExceptionController {
@RequestMapping(value="/testException", method=RequestMethod.GET)
public String testException(){
String s = null;
System.err.println(s.substring(0, 5));
return "success";
}
}
4.error.jsp
<a href="">操作异常,请稍后重试</a>
${exception}
5.运行结果