spring boot之spring mvc常用配置--@controllerAdvice(5)

@controllerAdvice:我们可以将对于使用@Controller注解的控制器的全局配置放在同一个位置。
对于注解了@Controller的类,该类下可以使用@ExceptionHandler、@InitBinder、@modelAttribute。@RequestMapping的控制器内的方法也是有效的。
而使用@controllerAdvice(控制器通知)注解后。这些@ExceptionHandler、@InitBinder、@modelAttribute这些全局注解
更方便的统一管理。
@ExceptionHandler:用户全局处理控制里的异常。
@InitBider:用户设置WebDataBinder,WebDataBinder用来自动绑定前台求求参数到Model中。
@ModelAttribute:@ModelAttribute本来的作用就是绑定键值对到Model里。此处是让全局的@RequestMapping都能获得在此处设置的键值对。
案例:使用@ExceptionHandler处理全局异常,更人性化的将异常输出给用户。
package com.boot.springmvc.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 //1
public class ExceptionHandlerAdvice { 

	@ExceptionHandler(value = Exception.class)//2
	public ModelAndView exception(Exception exception, WebRequest request) {
		ModelAndView modelAndView = new ModelAndView("error");// error页面
		modelAndView.addObject("errorMessage", exception.getMessage());
		return modelAndView;
	}

	@ModelAttribute //3
	public void addAttributes(Model model) {
		model.addAttribute("msg", "额外信息"); //3
	}

	@InitBinder //4
	public void initBinder(WebDataBinder webDataBinder) {
		webDataBinder.setDisallowedFields("id"); //5
	}
}
一:ExceptionHandlerAdvice类
1.@ControllerAdvice声明一个控制器建言,@ControllerAdvice组合了@Component注解,
所以自动注册未spring的Bean。
2.@ExceptionHandler在此处定义全局异常处理,通过@ExceptionHandler的value属性可以过滤拦截的条件,在此处拦截所有Exception。
3.此处使用@ModelAttribute注解将键值对天骄到全局,所有注解的@RequestMapping的方法可都可以获得此键值对。

4.通过@InitBinder注解定制WebDataBinder。(5.这里表示过滤掉id)

二:抛出异常演示

package com.boot.springmvc.web.ch2;

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

import com.boot.springmvc.domain.DemoObj;

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

}
三:异常页面展示

<%@ 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=UTF-8">
<title>@ControllerAdvice Demo</title>
</head>
<body>
	${errorMessage}
</body>
</html>

结果:

全局的@ModelAttribute,@RequestMapping都可以获取


webDataBinder.setDisallowedFields("id"); //过滤id属性


异常展示


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值