<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 一、頁面類異常處理 之前章節給大家講的都是接口類的異常處理,那我們做頁面模板時,Controller發生異常我們該怎么辦?應該統一跳轉到404頁面。 面臨的問題: **程序員拋出自定義異常CustomException,全局異常處理截獲之后返回@ResponseBody AjaxResponse,不是ModelAndView,所以我們無法跳轉到error.html頁面,那我們該如何做頁面的全局的異常處理?** 答: 1. 用面向切面的方式,將CustomException轉換為ModelAndViewException。 2. 全局異常處理器攔截ModelAndViewException,返回ModelAndView,即error.html頁面 3. 切入點是帶@ModelView注解的Controller層方法 **使用這種方法處理頁面類異常,程序員只需要在頁面跳轉的Controller上加@ModelView注解即可** ### 錯誤的寫法 ~~~ @GetMapping("/freemarker") public String index(Model model) { try{ List<ArticleVO> articles = articleRestService.getAll(); model.addAttribute("articles", articles); }catch (Exception e){ return "error"; } return "fremarkertemp"; } ~~~ ### 正確的寫法 ~~~ @ModelView @GetMapping("/freemarker") public String index(Model model) { List<ArticleVO> articles = articleRestService.getAll(); model.addAttribute("articles", articles); return "fremarkertemp"; } ~~~ ## 二 用面向切面的方法處理頁面全局異常 因為用到了面向切面編程,所以引入maven依賴包 ~~~ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ~~~ 新定義一個異常類ModelViewException ~~~ public class ModelViewException extends RuntimeException{ //異常錯誤編碼 private int code ; //異常信息 private String message; public static ModelViewException transfer(CustomException e) { return new ModelViewException(e.getCode(),e.getMessage()); } private ModelViewException(int code, String message){ this.code = code; this.message = message; } int getCode() { return code; } @Override public String getMessage() { return message; } } ~~~ ModelView 注解,只起到標注的作用 ~~~ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD})//只能在方法上使用此注解 public @interface ModelView { } ~~~ 以@ModelView注解為切入點,面向切面編程,將CustomException轉換為ModelViewException拋出。 ~~~ @Aspect @Component @Slf4j public class ModelViewAspect { //設置切入點:這里直接攔截被@ModelView注解的方法 @Pointcut("@annotation(com.kimgao.bootlauch.config.exception.ModelView)") public void pointcut() { } /** * 當有ModelView的注解的方法拋出異常的時候,做如下的處理 */ @AfterThrowing(pointcut="pointcut()",throwing="e") public void afterThrowable(Throwable e) { log.error("切面發生了異常:", e); if(e instanceof CustomException){ throw ModelViewException.transfer((CustomException) e); } } } ~~~ ![](https://img.kancloud.cn/a2/ca/a2caa342467825cfa0c24878e1dc9c52_470x367.png) 全局異常處理器: ![](https://img.kancloud.cn/9f/25/9f2593f1272d325221742626b942a8ef_1825x682.png) ~~~ @ExceptionHandler(ModelViewException.class) public ModelAndView viewExceptionHandler(HttpServletRequest req, ModelViewException e) { ModelAndView modelAndView = new ModelAndView(); //將異常信息設置如modelAndView modelAndView.addObject("exception", e); modelAndView.addObject("url", req.getRequestURL()); modelAndView.setViewName("error"); //返回ModelAndView return modelAndView; } ~~~ ## 三 測試 創建error.html ~~~ <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <!--thymeleaf favicon.ico y --> <link rel="shortcut icon" th:href="@{/favicon.ico}"/> <head lang="en"> <meta charset="UTF-8" /> <title>error頁面</title> </head> <body> <h1>error</h1> <div th:text="${exception}"></div> <div th:text="${url}"></div> </body> </html> ~~~ TemplateController中創建一個異常 引入異常service ~~~ @Resource ExceptionService exceptionService; ~~~ 制造一個異常,并返回ModelView ~~~ @ModelView @GetMapping("/thymeleaf") public String index(Model model, HttpSession session) { ... exceptionService.systemBizError(); ... ~~~ ![](https://img.kancloud.cn/70/b2/70b2e8742950801588c90cdc428a7a1e_888x500.png) 啟動服務,運行頁面 [http://127.0.0.1:8899/kimgao/template/thymeleaf](http://127.0.0.1:8899/kimgao/template/thymeleaf) 異常跳轉到的error頁面 ![](https://img.kancloud.cn/45/de/45de8bdc8bf923e52a1467a59f99e52c_1435x422.png) ## 四 總結 1. 全都用CustomException拋出異常,不在單獨寫try catch; 2. 模板、ajax、json混用可以加@ModelView跳轉到error頁面; 3. 服務端數據校驗,需要加@Valid。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看