工程代码

github: https://github.com/dengjili/springmvc

@ControllerAdvice

当执行控制器priv.dengjl.controller下面的类时候,下面的方法将被代理,前置aop、或后置aop

// 控制器里的aop,拦截指定包
@ControllerAdvice(basePackages = { "priv.dengjl.controller" })
public class AdviceController {

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// 运行为空
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}

	@ExceptionHandler(value = {Exception.class})
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("exception");
		return mv;
	}
}

@InitBinder

为priv.dengjl.controller包下的控制器注册日期转换

// 控制器里的aop,拦截指定包
@ControllerAdvice(basePackages = { "priv.dengjl.controller" })
public class AdviceController {

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// 运行为空
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}
}

控制器测试

	@RequestMapping("/format")
	public ModelAndView format(Date date, @NumberFormat(pattern = "#,###.00") Double amount) {
		logger.debug("date==> {}", date);
		logger.debug("amount==> {}", amount);

		ModelAndView mv = new ModelAndView();
		mv.setViewName("formatter");
		return mv;
	}

@ExceptionHandler

为priv.dengjl.controller包下的控制器添加异常处理

// 控制器里的aop,拦截指定包
@ControllerAdvice(basePackages = { "priv.dengjl.controller" })
public class AdviceController {

	@ExceptionHandler(value = {Exception.class})
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("exception");
		return mv;
	}
}

控制器测试

	@RequestMapping("/getException")
	public ModelAndView getException() throws Exception {
		if (true) {
			throw new Exception("test");
		}
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("advice");
		return mv;
	}

@ModelAttribute入门

http://localhost:8080/springmvc/advice/getRequestAttribute2

在进入控制器之前,执行ModelAttribute注解下面的方法,并临时存储数据

	// 在进入控制器之前执行
	@ModelAttribute("beanParam")
	public BeanParam testRequest2() {
		// 设置请求属性
		BeanParam param = new BeanParam();
		param.setName("张三22");
		param.setNote("test22");
		return param;
	}
	
	// @ModelAttribute读取
	@RequestMapping("/getRequestAttribute2")
	public ModelAndView getRequestAttribute2(@ModelAttribute("beanParam") BeanParam param) {
		logger.debug("name: {}", param.getName());
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("advice");
		return mv;
	}

@ModelAttribute与重定向

请求地址:http://localhost:8080/springmvc/advice/testRedirect

	@RequestMapping("/testRedirect")
	public String testRedirect(RedirectAttributes ra) {
		BeanParam param = new BeanParam();
		param.setName("张三");
		param.setNote("test");
		
		// 数据域
		ra.addFlashAttribute("param", param);
		return "redirect:./showPojo";
	}

	// 处理数据模型,如果返回对象,则对象会保持在ModelAttribute
	// 需要使用@ModelAttribute来接收参数
	@RequestMapping("/showPojo")
	public ModelAndView showPojo(@ModelAttribute("param") BeanParam param ) {
		logger.debug("name: {}", param.getName());
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("advice");
		return mv;
	}
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐