码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC3学习(九)--redirect和forward跳转

时间:2017-04-20 23:11:17      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:control   detail   strong   login   dir   无法   host   article   url   

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Login{
	
        /**转发**/
	@RequestMapping("/login.do")
	public String login(HttpServletRequest request,HttpServletResponse 

response){
		request.setAttribute("message", "hello");
		return "forward:/index.do";  //forward在跳转后能够取到message值
	}
	@RequestMapping("/index.do")
	public String index(HttpServletRequest request,HttpServletResponse 

response){
		return "welcome";
	}
	
	/**重定向**/
	@RequestMapping("/logout.do")
	public String logout(HttpServletRequest request,HttpServletResponse 

response){
		request.setAttribute("message", "hello");
		return "redirect:/register.do";  //redirect在跳转后无法取到message

值
	}
	@RequestMapping("/register.do")
	public String register(HttpServletRequest request,HttpServletResponse 

response){
		return "register";
	}
}

另外forward跳转后地址栏URL不会改变 而redirect会改变


測试URL:

http://localhost:8080/spring_forward/login.do

http://localhost:8080/spring_forward/logout.do


项目源代码:http://download.csdn.net/detail/itmyhome/7387639




springMVC3学习(九)--redirect和forward跳转

标签:control   detail   strong   login   dir   无法   host   article   url   

原文地址:http://www.cnblogs.com/cxchanpin/p/6741007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!