对于某些Controller的处理方法,当返回值为String类型时,返回的结果中可能含有forward或redirect前缀;
如:
@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中的参数不会丢失;
相关推荐
3.4. forward和redirect的问题 3.4.1. 绝对路径与相对路径 3.4.1.1. 相对路径 3.4.1.2. 绝对路径 3.4.2. forward导致找不到图片 4. 四个作用域 4.1. 何为作用域 4.2. 例子:在线列表 5. 结合javabean实现CRUD ...
6、SERVLET API中forward() 与redirect()的区别? 86 7、什么情况下调用doGet()和doPost()? 8、Request对象的主要方法: 87 9、forward 和redirect的区别 10、request.getAttribute() 和 request.getParameter() ...
6、SERVLET API中forward() 与redirect()的区别? 86 7、什么情况下调用doGet()和doPost()? 86 8、Request对象的主要方法: 87 9、forward 和redirect的区别 87 10、request.getAttribute() 和 request....
6、SERVLET API中forward() 与redirect()的区别? 86 7、什么情况下调用doGet()和doPost()? 86 8、Request对象的主要方法: 87 9、forward 和redirect的区别 87 10、request.getAttribute() 和 request....
6、SERVLET API中forward() 与redirect()的区别? 86 7、什么情况下调用doGet()和doPost()? 86 8、Request对象的主要方法: 87 9、forward 和redirect的区别 87 10、request.getAttribute() 和 request....
6、SERVLET API中forward() 与redirect()的区别? 86 7、什么情况下调用doGet()和doPost()? 86 8、Request对象的主要方法: 87 9、forward 和redirect的区别 87 10、request.getAttribute() 和 request....
6、SERVLET API中forward() 与redirect()的区别? 86 7、什么情况下调用doGet()和doPost()? 86 8、Request对象的主要方法: 87 9、forward 和redirect的区别 87 10、request.getAttribute() 和 request....
6、SERVLET API中forward() 与redirect()的区别? 86 7、什么情况下调用doGet()和doPost()? 86 8、Request对象的主要方法: 87 9、forward 和redirect的区别 87 10、request.getAttribute() 和 request....
6、SERVLET API中forward() 与redirect()的区别? 94 7、什么情况下调用doGet()和doPost()? 94 8、Request对象的主要方法: 94 9、forward 和redirect的区别 95 10、request.getAttribute() 和 request....
6、SERVLET API中forward() 与redirect()的区别? 86 7、什么情况下调用doGet()和doPost()? 86 8、Request对象的主要方法: 87 9、forward 和redirect的区别 87 10、request.getAttribute() 和 request....
forward与redirect区别 页面请求的工作流程 HTTP中的GET和POST方法有什么区别 什么是Servlet servlet是线程安全的吗 Servlet的生命周期 JSP和Servlet的区别和联系 什么是MVC模型 TCP的连接和释放过程 什么是长连接...
22、forward 和redirect的区别 forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所以它的地址...
7. forward 和redirect的区别 17 8. 说出在JSP页面里是怎么分页的? 17 9. 什么情况下调用doGet()和doPost()? 17 10. servlet的生命周期 17 11. Servlet执行时一般实现哪几个方法? 17 12. Servlet和CGI的区别。 18 13...
19、forward 和redirect的区别 forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所以它的地址栏...
12.5.1. iBATIS 1.x和2.x的概览与区别 12.5.2. iBATIS SQL Maps 1.x 12.5.2.1. 创建SqlMap 12.5.2.2. 使用 SqlMapTemplate 和 SqlMapDaoSupport 12.5.3. iBATIS SQL Maps 2.x 12.5.3.1. 创建SqlMapClient 12.5.3.2....