日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

SpringMVC異常處理器

作者:小豬.get 更新時間: 2022-08-19 編程語言

目錄

一、基于xml配置的異常處理

二、基于注解的異常處理

1.ControllerAdvice?源碼

2.ExceptionHandler?源碼

3.控制層方法?


一、基于xml配置的異常處理

SpringMVC提供了一個處理控制器方法執行過程中所出現的異常的接口:HandlerExceptionResolver

HandlerExceptionResolver的源碼

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.web.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.lang.Nullable;

public interface HandlerExceptionResolver {
    @Nullable
    ModelAndView resolveException
     (HttpServletRequest var1, 
     HttpServletResponse var2, 
     @Nullable Object var3, 
     Exception var4);
}

HandlerExceptionResolver接口的實現類有:

  • DefaultHandlerExceptionResolver
  • SimpleMappingExceptionResolver

SpringMVC提供了自定義的異常處理器SimpleMappingExceptionResolver,使用方式

@Controller
public class TestController {
    @RequestMapping("/test/hello")
    public String testHello(){
        System.out.println(1/0);
        return "success";
    }
}
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <!--key設置要處理的異常,value設置出現該異常時要跳轉的頁面對應的邏輯視圖-->
            <prop key="java.lang.ArithmeticException">error</prop>
        </props>
    </property>
    <!--設置共享在請求域中的異常信息的屬性名-->
    <property name="exceptionAttribute" value="ex"></property>
</bean>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>錯誤</title>
</head>
<body>
    <h1>error.html</h1>
    <p th:text="${ex}"></p>
</body>
</html>

二、基于注解的異常處理

1.ControllerAdvice?源碼

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice {
    @AliasFor("basePackages")
    String[] value() default {};

    @AliasFor("value")
    String[] basePackages() default {};

    Class<?>[] basePackageClasses() default {};

    Class<?>[] assignableTypes() default {};

    Class<? extends Annotation>[] annotations() default {};
}

2.ExceptionHandler?源碼

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExceptionHandler {
    Class<? extends Throwable>[] value() default {};
}

3.控制層方法?

package com.atguigu.controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

//將當前類標識為異常處理的組件
@ControllerAdvice
public class ExceptionController {
    //設置要處理的異常信息
    @ExceptionHandler(ArithmeticException.class)
    public String handleException(Throwable ex,Model model){
        //將異常信息共享到請求域,ex表示控制器方法出現的異常
        model.addAttribute("ex",ex);
        return "error";
    }
}

運行效果:?

?

原文鏈接:https://blog.csdn.net/m0_52982868/article/details/126403749

欄目分類
最近更新