首页 > 代码库 > springMVC3学习(九)--redirect和forward跳转
springMVC3学习(九)--redirect和forward跳转
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
欢迎加入JAVA技术交流群:74955800
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。