SpringMVC统一异常处理简单配置
jopen
10年前
第一步:自定义异常处理类实现HandlerExceptionResolver接口
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; public class SystemExceptionHandler implements HandlerExceptionResolver{ @Override public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse resp, Object obj, Exception ex) { // 根据不同的错误跳转到不同的页面 if (ex instanceof BusinessException) { return new ModelAndView("errors/error-business"); } else if (ex instanceof ParameterException) { return new ModelAndView("errors/error-parameter"); } else { return new ModelAndView("errors/error"); } } }
第二步:spring文件配置
<!-- spring统一异常处理拦截器 --> <bean id="exceptionHandler" class="com.my.exception.SystemExceptionHandler"/>
来自:http://my.oschina.net/u/1469821/blog/354531