1. 程式人生 > >springmvc 轉發Forward(springmvc教程四)

springmvc 轉發Forward(springmvc教程四)

目錄

工程程式碼

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

頁面檔案:forward.jsp

<html>
<body>
	<h2>Hello World!</h2>
	hehe
	<hr/>
	<a href="setRequest">setRequest</a>	
</body>
</html>

普通跳轉

	@RequestMapping("/setRequest")
	public ModelAndView setRequest(HttpServletRequest request) {
		
		// 設定請求屬性
		request.setAttribute("id", 1234);
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("forward:./getRequestAttribute");
		return mv;
	}
	
	@RequestMapping("/getRequestAttribute")
	public ModelAndView getRequestAttribute(@RequestAttribute("id") int id) {
		logger.debug("{}", id);
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("forward");
		return mv;
	}