标签:spring add dispatch error pat java 信息 void vax
1.SpringMVC异常处理的方式
、
2. 异常处理思路
1>. Controller调用service,service调用dao,异常都是向上抛出的,最终有DispatcherServlet找异常处理器进 行异常的处理。
3. SpringMVC的异常处理
1>. 自定义异常类
package com.hdh.utils;
public class SysException extends Exception {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public SysException(String message) {
super();
this.message = message;
}
}
2> 自定义异常处理器
package com.hdh.utils;
import javax.jws.WebParam.Mode;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
public class SysExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
// 获取异常
SysException e = null;
if (ex instanceof SysException) {
e = (SysException) ex;
}else {
e=new SysException("系统正在维护。。。");
}
ModelAndView mView=new ModelAndView();
//将错误的信息保存到errorMsg并存入request域中
mView.addObject("errorMsg",e.getMessage());
//跳转到异常处理的jsp
mView.setViewName("error");
return mView;
}
}
3. 配置异常处理器
<bean id="sysExceptionResolver" class="com.hdh.utils.SysExceptionResolver"></bean>
标签:spring add dispatch error pat java 信息 void vax
原文地址:https://www.cnblogs.com/asndxj/p/11381957.html