关于SpringMVC中的转发与重定向的说明
写的非常详细,参看该地址:https://www.zifangsky.cn/661.html
总结:
1.请求转发:url地址不变,可带参数,如?username=forward
2.请求重定向:url地址改变,在url上带参数无效。具体可以使用四种传参方式:
a.使用sesssion,b.使用RedirectAttribute类,c.使用@ModelAttribute注解,d.使用RequestContextUtils类(推荐使用后面两中)
参考:
转发:一次请求,服务器内部调用另外的组件处理,request和response可以共用,有限制性,只能转发到本应用中的某些资源,页面或者controller请求,可以访问WEB-INF目录下面的页面
重定向:两次请求,地址会改变,request和response不能共用,不能直接访问WEB-INF下面的资源,
根据所要跳转的资源,可以分为跳转到页面或者跳转到其他controller

实例代码(在springboot下测试的)如下:
/**
* @Author Mr.Yao
* @Date 2019/5/5 10:22
* @Content SpringBootStudy
*/
@Controller
public class ForwardAndRedirectController {
@RequestMapping("/test/index")
public ModelAndView userIndex() {
System.out.println("进入userIdex了");
ModelAndView view = new ModelAndView("index");
view.addObject("name","向html页面中设值。");
return view;
} //使用forward
@RequestMapping("/testForward.html")
public ModelAndView testForward(@RequestParam("username") String username){ System.out.println("test-forward....."+username);
ModelAndView mAndView = new ModelAndView("forward:/test/index"); User user = new User();
user.setName(username);
mAndView.addObject("user", user);
return mAndView;
}
//使用servlet api
@RequestMapping(value="/test/api/{name}")
public void test(@PathVariable String name, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("使用servlet api中的方法。。。"+name);
request.getRequestDispatcher("/test/index").forward(request, response);
} //使用redirect
@RequestMapping("/testRedirect.html")
public ModelAndView testRedirect(@RequestParam("username") String username){
ModelAndView mAndView = new ModelAndView("redirect:/redirect/index");
System.out.println("test-redirect....."+username);
User user = new User();
user.setName(username);
mAndView.addObject("user", user);
mAndView.addObject("name", "hello world");
return mAndView;
}
@RequestMapping("/redirect/index")
public ModelAndView indexRedirect(@ModelAttribute("user") User user, @ModelAttribute("name") String name) { System.out.println(name +"====通过重定向过来的,获取参数值:"+user.getName());
return new ModelAndView("index");
}
//使用servlet api 中重定向,responese.sendRedirect()
}
关于SpringMVC中的转发与重定向的说明的更多相关文章
- java web 中的转发和重定向
假设应用程序的 contextPath 为 /ctx,在 http://localhost:8080/ctx/a/b 资源中,我们转发和重定向到 http://localhost:8080/ctx/x ...
- Servlet中的转发与重定向
Sevlet 的转发与重定向都可以使得浏览器指向另一个资源文件,但它们的运行机制不相同. 一.Servlet的转发 有两种方式获得转发对象(RequestDispathcer): HttpServle ...
- Java Web开发中的转发和重定向的问题
Java Web的页面实现跳转有两种方式,一种是转发,另外一种是重定向.一般来说,转发比重定向快.重定向会经过客户端,转发却不会. 转发 request.getRequestDispatcher(&q ...
- servlet中的转发和重定向问题
重定向和请求转发在学习servlet的时候很容易混淆,故在此特意记录. 1. 重定向---------sendRedirect()方法 Servlet响应请求有两种方式,一个是重定向,返回一个页面给客 ...
- 12.SpringMVC核心技术-请求转发和重定向
默认情况下,跳转到指定的View,使用的是请求转发.也可以显示的进行指出 此时,需在setViewName() 指定的视图前添加 forword: , 且此时的视图不会再与视图解析器中的前缀和后缀进 ...
- SpringMVC传值、转发、重定向例子
练习接收页面参数值 使用request 使用@RequestParam注解 使用实体对象 练习向页面传出数据 使用HttpServletRequest和session 使用ModelAndView对象 ...
- javaweb中关于转发与重定向的写法
转发: RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/main.jsp"); rd.forward(r ...
- SpringMVC中的重定向和转发的实现
1.请求转发和重定向的区别 请求重定向和请求转发都是web开发中资源跳转的方式. 请求转发是服务器内部的跳转 地址栏比发生变化 只有一个请求相应 可以通过request域对跳转目标的请求 请求重定向是 ...
- SpringMVC转发和重定向区别!
在servlet中,转发和重定向是由request和response完成的.两者之间的区别请看我之前的文章.那么在springMVC中是如何完成的呢? /**转发**/ @RequestMapping ...
随机推荐
- codevs 1166 矩阵取数游戏
二次联通门 : codevs 1166 矩阵取数游戏 /* codevs 1166 矩阵取数游戏 SB区间dp dp[l][r] = max (dp[l + 1][r] + number[l], dp ...
- python+opencv 图像预处理
一 python 生成随机字符串序列+ 写入到图片上 from PIL import Image,ImageDraw,ImageFont import numpy as np import rando ...
- 内存管理2-set方法的内存管理
1.对象之间的内存管理: 每个学生都有一本书 book类 @price 学生类 @age @book -------------------- #import "book.h" ...
- DELPHI10.3.1安卓照相
DELPHI10.3.1安卓照相 解决方法:
- 验证HashSet和HashMap不是线程安全
JAVA集合类: java.util包下的HashSet和HashMap类不是线程安全的, java.util.concurrent包下的ConcurrentHashMap类是线程安全的. 写2个测试 ...
- 【English】What is a Java StringWriter, and when should I use it?(转帖)
转帖地址:http://www.it1352.com/989366.html Question: What is a Java StringWriter, and when should I use ...
- 【SpringBoot】整体下载大文件与分批下载大文件(利用MySql数据库的Limit实现)
在前文里谈过一次性从数据库取一个大结果集有可能导致outofMemory,当时的想法是分批去取回来,今天把它实现了,特地把代码分享出来: 工程下载:https://files.cnblogs.com/ ...
- Python 调用outlook发送邮件(转 )
单账号: import win32com.client as win32 def send_mail(): outlook = win32.Dispatch('Outlook.Application' ...
- .IllegalArgumentException: Mapped Statements collection does not contain 异常一例【我】
更新代码后发现几乎所有的sql查询都报错,类似下面: java.lang.RuntimeException: org.mybatis.spring.MyBatisSystemException: ne ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_6.RabbitMQ研究-入门程序-消费者
我们在consumer这个功能下进行代码的编写 首先是新建这个层级的包 创建入门程序的消费者 消费者也需要和mq建立通道.建立连接创建通道 在顶部都声明这个队列 下面写核心代码监听队列.basicCo ...