BusinessException.java
package exception_01;
public class BusinessException extends Exception
{
private static final long serialVersionUID = 2201800548165425255L;
}
ParameterException.java
package exception_01;
public class ParameterException extends Exception
{
private static final long serialVersionUID = 745096881594085953L;
}
SystemExceptionHandler.java
package exception_01;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
/*
* <!-- spring统一异常处理拦截器 -->
* <bean id="exceptionHandler" class="com.my.exception.SystemExceptionHandler"/>
*/
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");
}
}
}