第一步:自定义异常处理类实现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文件配置