forward:hello 与 redirect:hello的区别
2014-08-19 21:28
483 查看
对于某些Controller的处理方法,当返回值为String类型时,返回的结果中可能含有forward或redirect前缀;
如:
测试页面hello.jsp:
当路径为 http://localhost:8080/crazysnailweb/user/forward?userId=123时,浏览器中的URL不会发生变化,且页面输出:
当路径为http://localhost:8080/crazysnailweb/user/redirect?userId=123 时,浏览器中URL变为http://localhost:8080/crazysnailweb/user/hello,且页面输出:
注:redirect会让浏览器发起一个新的请求,因而原来request对象中的参数丢失;而forward所到的目标地址位于当前请求中,request中的参数不会丢失;
如:
@Controller @RequestMapping("/user") public class UserController { @RequestMapping("/forward") public String replyWithForward(HttpServletRequest request, String userId){ request.setAttribute("userid", userId); System.out.println("userId =" +userId); return "forward:hello"; } @RequestMapping("/redirect") public String replyWithRedirect(HttpServletRequest request, String userId){ request.setAttribute("userid", userId); System.out.println("userId = "+userId); return "redirect:hello"; } }
测试页面hello.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String userId = (String)request.getAttribute("userid"); System.out.println("获取到的userId为:"+userId); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试页面</title> </head> <body> hello, <%= userId %>; </body> </html>
当路径为 http://localhost:8080/crazysnailweb/user/forward?userId=123时,浏览器中的URL不会发生变化,且页面输出:
当路径为http://localhost:8080/crazysnailweb/user/redirect?userId=123 时,浏览器中URL变为http://localhost:8080/crazysnailweb/user/hello,且页面输出:
注:redirect会让浏览器发起一个新的请求,因而原来request对象中的参数丢失;而forward所到的目标地址位于当前请求中,request中的参数不会丢失;
相关文章推荐
- forward:hello 与 redirect:hello的区别
- JSP中forward和redirect的区别
- Jsp中Forward和Redirect的区别。
- 请求的跳转与转发 forward, redirect的区别
- forward与redirect转发的区别
- forward和redirect的区别
- response.redirect和pageContext.forward的区别
- forward和redirect的区别
- forward与redirect的区别
- 再看 forward 和 redirect 的区别及过程
- forward和redirect的区别
- forward,include,redirect区别。
- include、forward 和 redirect的区别
- forward与redirect的区别
- forward 和redirect的区别
- Forward与Redirect区别
- Servlet/JSP服务器端的重定向(forward,include,redirect区别)
- forward和redirect的区别
- forward和redirect的区别
- Redirect和Forward区别