<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error.jsp"/>
<property name="exceptionMappings">
<props>
<prop key="java.lang.ArithmeticException">error1.jsp</prop>
<prop key="java.lang.NullPointerException">error2.jsp</prop>
</props>
</property>
</bean>
<!--注册bean-->
<bean class="com.MyExceptionResolver"/>
package com;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
if(e instanceof ArithmeticException){
modelAndView.setViewName("error1.jsp");
}else if(e instanceof NullPointerException){
modelAndView.setViewName("error2.jsp");
}else {
modelAndView.setViewName("error.jsp");
}
return modelAndView;
}
}
原文:https://www.cnblogs.com/kwdlh/p/13579686.html