controlleradvice 参数_SpringMvc4.x基本配置(三):@ControllerAdvice注解

一. 点睛

通过@ControllerAdvice。我们可以将对于控制器的全局配置放置在同一个位置,注解了@ControllerAdvice的类的方法可以使用@ExceptionHandler,@InitBinder,@ModelAttribute注解到方法上,这对所有注解了@RequestMapping的控制器内的方法有效。

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

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

@ModelAttribute:@ModelAttribute本来的作用是绑定键值对到Model里,此处是让全局的@RequestMapping都能获得在此处设置的键值对。

下面将使用@ExceptionHandler处理全局异常,将异常信息更加人性化的输出给用户。

二. 示例

1. 定制@ControllerAdvice

package org.light4j.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 //①

public class ExceptionHandlerAdvice {

@ExceptionHandler(value = Exception.class)//②

public ModelAndView exception(Exception exception, WebRequest request) {

ModelAndView modelAndView = new ModelAndView("error");// error页面

modelAndView.addObject("errorMessage", exception.getMessage());

return modelAndView;

}

@ModelAttribute //③

public void addAttributes(Model model) {

model.addAttribute("msg", "额外信息"); //④

}

@InitBinder //④

public void initBinder(WebDataBinder webDataBinder) {

webDataBinder.setDisallowedFields("id"); //⑤

}

}

代码解释:

① @ControllerAdvice声明一个控制器建言,@ControllerAdvice组合了@Component注解,所以自动注册为Spring的Bean。

② @ExceptionHandler在此处定义全局处理,通过@ExceptionHandler的value属性可过滤拦截的条件,在此处可以看出拦截的是所有的Exception。

③ 此处使用@ModelAttribute注解将键值对添加到全局,所有注解了@RequestMapping的方法可获得此键值对。

④ 通过@InitBinder注解定制WebDataBinder。

下面演示忽略request参数的id,更多关于WebDataBinder的配置,请参考WebDataBinder的API文档。

2. 演示控制器

package org.light4j.springMvc4.web;

import org.light4j.springMvc4.domain.DemoObj;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class AdviceController {

@RequestMapping("/advice")

public String getSomething(@ModelAttribute("msg") String msg,DemoObj obj){//①

throw new IllegalArgumentException("非常抱歉,参数有误/"+"来自@ModelAttribute:"+ msg);

}

}

3. 异常展示页面

在src/main/resources/views下,新建error.jsp,内容如下:

pageEncoding="UTF-8"%>

@ControllerAdvice Demo

${errorMessage}

4. 运行

访问http://localhost/springMvc4.x-advice/advice?id=1&name=xxx。调试查看DemoObj,id被过滤掉了,

如下图所示:

且获得了@ModelAttribute的msg信息,

如下图所示:

页面效果如下图所示:

注意点:我在测试的时候,error.jsp里面el表达式的值取不出来,拿不到后台传过来的值。后来找到的原因是:web.xml的头内容版本是2.3的话默认jsp不开启el解析,需要开启解析,在error.jsp头部增加如下配置:

增加上面的配置之后问题就解决了。

三. 源代码示例:

github地址:点击查看

码云地址:点击查看

打赏

微信扫一扫,打赏作者吧~欢迎关注人生设计师的微信公众账号

公众号ID:longjiazuoA

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值