转自:https://blog.csdn.net/qiuzhi__ke/article/details/50951117
return new ModelAndView(new RedirectView(this.getViewPage()));
return new ModelAndView(\"redirect:/center/index.do\");
new ModelAndView(new RedirectView(\"xxx.do\"),\"modelName\",modelMap);
new ModelAndView(new RedirectView(\"xxx.do?msg=xyz\");
String 指定返回的视图页面名称,结合设置的返回地址路径加上页面名称后缀即可访问到。
可以通过redirect/forward:url方式转到另一个Action进行连续的处理。
可以通过redirect:url 防止表单重复提交 。
写法如下:
return \"forward:/center/index.do\";
return \"redirect:/index.jsp\";
return \"redirect:select.do\";
return \"forward:select.do?code=200&status=ok!\";
return \"redirect:/user/select.do\";
redirect:这个关键字,这个关键字后面的就是你要重定向的请求路径,
在同一controller中则不用使用\"/\"从根目录开始,而如果是在不同的controller则一定要从根目录(加“/”)开始。
带参数重定向--RedirectAttributes
用户保存或修改后,为了防止用户刷新浏览器(F5)导致表单重复提交,一般在保存或修改操作之后会redirect到一个结果页面(不是forward),
同时携带参数,如操作成功的提示信息。因为是Redirect,Request里的attribute不会传递过去。Spring在3.1才提供了这个能力--RedirectAttributes。
反复按F5,操作成功的提示信息也不会再次出来(总共只出现一次),效果很理想。
public String save(@ModelAttribute(\"group\") Group group, RedirectAttributes redirectAttributes) {
accountManager.saveGroup(group);
redirectAttributes.addFlashAttribute(\"message\", \"操作成功\");
return \"redirect:/account/group/\";
}
@RequestMapping(value=\"/save.do\", method=RequestMethod.POST)
public ModelAndView saveInfo(HttpServletRequest request, HttpServletResponse response){
return new ModelAndView(\"center/index\");
response.sendRedirect(request.getContextPath() + \"/service/record/list.do\");
return null;
return new ModelAndView(\"redirect:\" + \"/center/index.do\");
return new ModelAndView(\"forward:\" + \"/center/index.do\");
}
@RequestMapping(value=\"/save.do\", method=RequestMethod.POST)
public String saveInfo(HttpServletRequest request, HttpServletResponse response){
return \"redirect:/account/index.do\";
return request.getContextPath() + \"/service/record/list.do\";
return \"redirect:/member/updateHead.do?url=\"+ url + \"&errCode=\" + errCode;
return \"v2/center/index\"; (index视图名字)
}
@RequestMapping(value=\"/save.do\", method=RequestMethod.POST)
public void saveInfo(HttpServletRequest request, HttpServletResponse response){
response.sendRedirect(request.getContextPath() + \"/account/login.do\");
request.getRequestDispatcher(\"/center/index.do\").forward(request, response);
}
ServletRequest接口中的getRequestDispatcher()方法的参数不但可以是相对于上下文根的路径,而且可以是相对于当前Servlet的路径。
例如:/myservlet和myservlet都是合法的路径,如果路径以斜杠(/)开始,则被解释为相对于当前上下文根的路径;
如果路径没有以斜杠(/)开始,则被解释为相对于当前Servlet的路径。
sendRedirect能够处理相对 URL,自动把它们转换成绝对URL,如果地址是相对的,没有一个‘/’,那么Web container就认为它是相对于当前的请求URI的。
比如,如果为response.sendRedirect(\"login.jsp\"),则会从当前servlet 的URL路径下找login.jsp: http://10.1.18.8:8081/dms/servlet/Servlet
重定向的URL: http://10.1.18.8:8081/dms/servlet/login.jsp.
如果为response.sendRedirect(\" /login.jsp\")则会从当前应用径下查找url:http://10.1.18.8:8081/login.jsp。
request.getRequestDispatcher(url).forward(request,response)是直接将请求转发到指定URL,所以该请求
能够直接获得上一个请求的数据,也就是说采用请求转发,request对象始终存在,不会重新创建。
而response.sendRedirect(url)会新建request对象,所以上一个request中的数据会丢失。
redirect 会首先发一个response给浏览器, 然后浏览器收到这个response后再发一个requeset给服务器, 然后
服务器发新的response给浏览器. 这时页面收到的request是一个新从浏览器发来的.
forward 发生在服务器内部, 在浏览器完全不知情的情况下发给了浏览器另外一个页面的response. 这时页面
收到的request不是从浏览器直接发来了,可能己经用request.setAttribute在request里放了数据.在转到的页
面可直接用request.getAttribute获得数据。
运用forward方法只能重定向到同一个Web应用程序中的一个资源。而sendRedirect方法可以让你重定向到任何URL。
使用response.sendRedirect()地址栏将改变
使用request.getRequestDispatcher().forward(request,response)地址栏中的信息保持不变.
Servlet中,\"/\"代表Web应用的跟目录。例如:\"./\" 代表当前目录,\"../\"代表上级目录。这种类似的表示,也是属于相对路径。
如果方法声明了注解 @ResponseBody,则会直接将返回值输出到页面
@RequestMapping(value = \"/something\", method = RequestMethod.GET)
@ResponseBody
public String helloWorld() {
return\"Hello World\";
}
上面的结果会将文本\"Hello World \"直接写到http响应流。
@RequestMapping(\"/welcome\")
public String welcomeHandler() {
return \"center\";
}
对应的逻辑视图名为“center”,URL= prefix前缀+视图名称 +suffix后缀组成。
继续阅读与本文标签相同的文章
5G未来终端设备可能只剩下显示屏?
数组学习总结
-
国内MRO公务机定检维修精细化管理探讨
2026-05-18栏目: 教程
-
初心不忘,创利心选
2026-05-18栏目: 教程
-
大陆集团:仓储智慧化建设是我国物流行业降本增效最有效的途径之一
2026-05-18栏目: 教程
-
5G真的来了!2020年将在超340个城市覆盖5G
2026-05-18栏目: 教程
-
Windows 10计算器应用更新:完全支持三角函数运算
2026-05-18栏目: 教程
