SpringBoot实战学习(四) SpringMVC基本配置2:@ControllerAdvice

1.目录结构

这里写图片描述

2.说明

@ControllerAdvice:控制器建言,对控制器全局配置,注解了@Controller的类的方法可使用@ExceptionHandler、@ModelAttribute、@InitBinder注解到方法上,对所有注解了@RequestMapping的控制器内的方法都有效

@ExceptionHandler:用于全局处理控制器的异常

@InitBinder :用来设置WebDataBinder,WebDataBinder用来自动绑定前台请求参数到Model中

@ModelAttribute: 绑定键值对到Model

3.编写

1.实体类

package com.wen.springmvc4.domain;

public class DemoObj {

    private Long id;
    private String name;

    public DemoObj() {
        super();
    }

    public DemoObj(Long id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }



}

2.定制ControllerAdvice

package com.wen.springmvc4.advice;

import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice//声明一个控制器建言,@ControllerAdvice组合了@Component注解,自动注册为Spring的Bean
public class ExceptionHandlerAdvice {

    //@ExceptionHandler在此定义全局处理,通过@ExceptionHandler的value属性可获得可过滤拦截的条件,此处拦截所有Exception
    @ExceptionHandler(value=Exception.class)
    public ModelAndView exception(Exception exception,WebRequest request){
        System.out.println("ExceptionHandlerAdvice-@ExceptionHandler");
        ModelAndView modelAndView=new ModelAndView("error");//error页面
        modelAndView.addObject("errorMessage",exception.getMessage());
        return modelAndView;
    }

    @ModelAttribute//将键值对添加到全局,所有注解的@RequestMapping的方法可获得此键值对
    public void addAttributrs(Model model){
        System.out.println("ExceptionHandlerAdvice-@ModelAttribute");
        model.addAttribute("msg", "额外消息");
    }

    @InitBinder//定制webDataBinder
    public void initBinder(WebDataBinder webDataBinder){
        System.out.println("ExceptionHandlerAdvice-@InitBinder");
        //此处忽略request参数参数的id
        webDataBinder.setDisallowedFields("id");

    }
}

3.控制器

package com.wen.springmvc4.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wen.springmvc4.domain.DemoObj;


@Controller
public class AdviceController {

    @RequestMapping("/advice")
    public String getSomething(@ModelAttribute("msg") String msg,DemoObj obj){
        System.out.println("AdviceController");
        throw new IllegalArgumentException("非常抱歉,参数有误/"+"来自@ModelAttribute:"+msg);
    }

}

4.异常展示页面
在src/main/resource/views先新建error.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>@ControllerAdvice Demo</title>
</head>
<body>
      ${errorMessage}
</body>
</html>

4.运行

将程序部署到Tomcat,运行,访问http://localhost:8080/SpringMVC/advice?id=1&name=2
这里写图片描述
控制台打印消息 :
这里写图片描述
可以看出先执行注解了@ModelAttribute、@InitBinder的方法,然后注解了@Controller类的@RequestMapping的方法,最后@ExceptionHandler

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值