本文中只測試了, 異常處理接口HandlerExceptionResolver
步驟
1.自定義異常類:
/** * 自定義全局異常處理類 */ public class CustomerExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { return new ModelAndView("500", "message", ex.getMessage()); } }
2.springMvc配置文件中增加
<!-- 注冊全局異常處理類 --> <bean class="zpark.exception.CustomerExceptionResolver"/>
3.controller
@RequestMapping("/test") public String test(){ throw new RuntimeException("出錯了,請修復.."); }
4.異常頁面捕獲異常信息
系統異常:${message }