SpringMVC异常处理
Spring处理异常(spring处理)
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="/jsp/error.jsp"/>
<property name="exceptionMappings">
<map>
<entry key="java.lang.NullPointerException" value="/jsp/errorNullPointerException.jsp"/>
<entry key="java.lang.ClassCastException" value="/jsp/errorClassCastException.jsp"/>
</map>
</property>
</bean>
自定义异常处理机制
springMVC配置
<bean class="com.resolver.MyExceptionResolver"/>
自定义异常处理类
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 NullPointerException){
modelAndView.addObject("info", "NullPointerException");
}else if (e instanceof ClassCastException){
modelAndView.addObject("info", "ClassCastException");
}
modelAndView.setViewName("/jsp/myError.jsp");
return modelAndView;
}
}