springMvc--全局異常處理


本文中只測試了, 異常處理接口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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM