@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



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值