第一步:自定义异常处理类实现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"/>
时间: 2024-10-06 22:58:33