1. 程式人生 > >springmvc Controller中的通知(aop)(springmvc教程十二)

springmvc Controller中的通知(aop)(springmvc教程十二)

目錄

工程程式碼

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;
	}