天天看點

@ControllerAdvice用法源碼用法執行個體

源碼

@ControllerAdvice,是Spring3.2提供的新注解,從名字上可以看出大體意思是控制器增強。讓我們先看看@ControllerAdvice的實作:

package org.springframework.web.bind.annotation;

@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 {};           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

該注解使用@Component注解,這樣的話當我們使用< context:component-scan>掃描時就能掃描到。

如果你的spring-mvc配置檔案使用如下方式掃描bean

<context:component-scan base-package="com.sishuok.es" use-default-filters="false">  
           <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
</context:component-scan>             
  • 1
  • 2
  • 3

還需要把@ControllerAdvice包含進來,否則不起作用:

<context:component-scan base-package="com.sishuok.es" use-default-filters="false">  
           <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
           <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
</context:component-scan>             
  • 1
  • 2
  • 3
  • 4

用法

  • 通過@ControllerAdvice注解可以将對于控制器的全局配置放在同一個位置。
  • 注解了@ControllerAdvice的類的方法可以使用@ExceptionHandler、@InitBinder、@ModelAttribute注解到方法上。
    • @ExceptionHandler:用于全局處理控制器裡的異常。
    • @InitBinder:用來設定WebDataBinder,用于自動綁定前台請求參數到Model中。
    • @ModelAttribute:本來作用是綁定鍵值對到Model中,此處讓全局的@RequestMapping都能獲得在此處設定的鍵值對
  • @ControllerAdvice注解将作用在所有注解了@RequestMapping的控制器的方法上。

執行個體

package org.lhzhian.base.exception;    

import org.springframework.web.bind.annotation.ControllerAdvice;    
import org.springframework.web.bind.annotation.ExceptionHandler;     
import org.springframework.web.servlet.ModelAndView;    

/**  
 * 異常統一處理  
 * @author lhzhian 
 * @date 2016年4月28日  
 */    
@ControllerAdvice    
public class GlobalExceptionHandler {    

    private final static String ERROR_PAGE = "error";    

    @ExceptionHandler(Exception.class)    
    public ModelAndView handle(Exception e){     
        ModelAndView mv = new ModelAndView();    
        mv.addObject("message", e.getMessage());    
        mv.setViewName(ERROR_PAGE);    
        return mv;    
    }    

    @ModelAttribute
    //應用到所有@RequestMapping注解方法
    //此處将鍵值對添加到全局,注解了@RequestMapping的方法都可以獲得此鍵值對
    public void addUser(Model model) { 
        model.addAttribute("msg", "此處将鍵值對添加到全局,注解了@RequestMapping的方法都可以獲得此鍵值對");
    }  

    @InitBinder  
    //應用到所有@RequestMapping注解方法,在其執行之前初始化資料綁定器
    //用來設定WebDataBinder,用于自動綁定前台請求參數到Model中。
    public void initBinder(WebDataBinder binder) {  
    } 

}            
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

error頁面

<%@ page language="java" contentType="text/html; charset=UTF-8" %> 

    <!DOCTYPE html"> 

    <html> 

    <head> 

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 

    <title>Insert title here</title> 

    </head> 

    <body> 

        <h1>ERROR MESSAGE</h1> 

        <p>${message}</p> 

    </body> 

    </html>             
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

繼續閱讀