写的非常详细,参看该地址: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中的转发与重定向的说明的更多相关文章

  1. java web 中的转发和重定向

    假设应用程序的 contextPath 为 /ctx,在 http://localhost:8080/ctx/a/b 资源中,我们转发和重定向到 http://localhost:8080/ctx/x ...

  2. Servlet中的转发与重定向

    Sevlet 的转发与重定向都可以使得浏览器指向另一个资源文件,但它们的运行机制不相同. 一.Servlet的转发 有两种方式获得转发对象(RequestDispathcer): HttpServle ...

  3. Java Web开发中的转发和重定向的问题

    Java Web的页面实现跳转有两种方式,一种是转发,另外一种是重定向.一般来说,转发比重定向快.重定向会经过客户端,转发却不会. 转发 request.getRequestDispatcher(&q ...

  4. servlet中的转发和重定向问题

    重定向和请求转发在学习servlet的时候很容易混淆,故在此特意记录. 1. 重定向---------sendRedirect()方法 Servlet响应请求有两种方式,一个是重定向,返回一个页面给客 ...

  5. 12.SpringMVC核心技术-请求转发和重定向

    默认情况下,跳转到指定的View,使用的是请求转发.也可以显示的进行指出 此时,需在setViewName()  指定的视图前添加 forword: , 且此时的视图不会再与视图解析器中的前缀和后缀进 ...

  6. SpringMVC传值、转发、重定向例子

    练习接收页面参数值 使用request 使用@RequestParam注解 使用实体对象 练习向页面传出数据 使用HttpServletRequest和session 使用ModelAndView对象 ...

  7. javaweb中关于转发与重定向的写法

    转发: RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/main.jsp"); rd.forward(r ...

  8. SpringMVC中的重定向和转发的实现

    1.请求转发和重定向的区别 请求重定向和请求转发都是web开发中资源跳转的方式. 请求转发是服务器内部的跳转 地址栏比发生变化 只有一个请求相应 可以通过request域对跳转目标的请求 请求重定向是 ...

  9. SpringMVC转发和重定向区别!

    在servlet中,转发和重定向是由request和response完成的.两者之间的区别请看我之前的文章.那么在springMVC中是如何完成的呢? /**转发**/ @RequestMapping ...

随机推荐

  1. go - GOPATH配置

    查看配置信息 go env 然后找到对应的 GOPATH=>修改为你自己的目录 ..liunx 系统下,注意要给权限 go bulid 编译文件 go run  先编译后执行. -------- ...

  2. lodop第三方插件的使用

    原文链接 使用pdf.js插件与LODOP控件实现前端浏览器静默打印PDF文件 lodop官网地址:http://www.lodop.net/download.html lodop他人总结教程:htt ...

  3. 7月清北学堂培训 Day 4

    今天是丁明朔老师的讲授~ 图论 图是种抽象结构,这种抽象结构可以表示点与点之间的关系. 最短路: Dijkstra(堆优化) SPFA Floyd 最小生成树: Kruscal 连通性: BFS / ...

  4. HTML页面预览表格文件内容

    背景简介 在将一个表格文件上传到服务器上之前,JS读取表格文件并将文件内容输出到页面中 vue项目 第三方 exceljs 安装 npm install exceljs 插件使用 github 中文文 ...

  5. fluent中统计颗粒信息【转载】

    转载自:http://mp.weixin.qq.com/s?__biz=MjM5MDkyMjA1Nw==&mid=502657725&idx=1&sn=120703a69e56 ...

  6. 攻防世界RE1 writeup

    解题过程 将题目给出的exe文件拖入ida中,查看main函数. 分析函数的逻辑,发现用户需要输出一个字符串,存储到变量v9中.如果v9的值与v5的值相等则会打印unk_413e90中的值,否则打印a ...

  7. the requested PHP extension dom is missing from your system

    composer  出错 the requested PHP extension dom is missing from your system 解决办法    yum install  php70w ...

  8. git仓库与项目源码分离

    在服务器上初始化git仓库 cd mkdir gitrepo cd gitrepo git init --bare runoob.git 本地 若是已经有git项目了,直接添加一个仓库地址就行了 gi ...

  9. OpenStack v.s. Kubernetes

    目录 文章目录 目录 What are the differences with OpenStack and Kubernetes? Why OpenStack & Kubernetes? W ...

  10. kvm管理工具Webvirtmgr安装

    虚拟机版本vmware workstation 15.5.0 pro   (也就是linux版) cat /etc/redhat-release CentOS Linux release 7.4.17 ...