关键词:
Web UI项目中, 很多 Spring controller 视图函数直接返回 html 页面, 还有一些视图函数是要重定向或转发到其他的 url 上.
redirect 和 forward的区别:
重定向 redirect: 完整的重定向包含两次request-response过程, 第一次是访问原始url, 第二次是服务器通知客户端访问重定向后的url. 重定向完成后, 浏览器的地址是重定向后的url, 而不是原始的url.
重定向的使用场景: 因为重定向会修改浏览器地址, 所以 form 提交应该使用重定向, 以免用户刷新页面导致form重复提交.
转发 forward: 完整的转发仅包含一次 request-response 过程, 用户发出request后, 服务器端视图函数先处理自己的逻辑, 然后在服务器端有调用另一个视图函数, 最后将response返回给浏览器.
==============================
转发 forward
==============================
在Spring MVC 中, 构建forward 目标有两种方式:
1. 以字符串的形式构建目标url, url 需要加上 forward: 前缀
2. 使用 ModelAndView 对象来设置转发的forward目标, viewName 可以省略 forward: 前缀, viewName 应该是目标url, 而不是目标视图的函数名.
传参方式:
1. 以字符串的形式构建目标url, 可以使用 query variable的格式拼url
2. 使用 ModelAndView 对象来增加 attribute Object, 其结果也是在拼接url.
取参的方式: 可以使用 @RequestParam 来取参.
==============================
重定向 redirect
==============================
redirect 目标有三种构建方式
1. 使用 redirect: 前缀url方式构建目标url
2. 使用 RedirectView 类型指定目标, 推荐使用这个,
3. 使用 ModelAndView 类型指定目标, ModelAndView 视图名默认是forward, 所以对于redirect, 需要加上 redirect: 前缀
传参和取参方式:
1. 传参: 以字符串的形式构建目标url, 可以使用 query variable的格式拼url. 取参: @RequestParam()来fetch
2. 传参: redirectAttributes.addAttribute() 加的attr. 取参: @RequestParam()来fetch
3. 传参: redirectAttributes.addFlashAttribute() 加的attr. 取参: @ModelAttribute()来fetch
Flash attribute的特点:
1. addFlashAttribute() 可以是任意类型的数据(不局限在String等基本类型), addAttribute()只能加基本类型的参数.
2. addFlashAttribute() 加的 attr, 不会出现在url 地址栏上.
3. addFlashAttribute() 加的 attr, 一旦fetch后, 就会自动清空, 非常适合 form 提交后 feedback Message.
==============================
示例代码
==============================
-----------------------------
pom.xml 和 application.properties
-----------------------------
<dependency> <groupId>io.pebbletemplates</groupId> <artifactId>pebble-spring-boot-2-starter</artifactId> <version>3.0.5</version> </dependency>
# application.properties file pebble.prefix=/templates/ pebble.suffix=.html pebble.content-type=text/html pebble.cache=false pebble.encoding=UTF-8 pebble.defaultLocale=null pebble.strictVariables=false
-----------------------------
java 代码
-----------------------------
@Controller @RequestMapping("/") public class DemoController { /* * forward 示例: 以字符串的形式构建目标url, url 需要加上 forward: 前缀 * */ @RequestMapping("/forwardTest1") public String forwardTest1() { return "forward:/forwardTarget?param1=v1¶m2=v2"; } /* * forward 示例: 使用 ModelAndView() 设置转发的目标url * */ @RequestMapping("/forwardTest2") public ModelAndView forwardTest2() { ModelAndView mav=new ModelAndView("/forwardTarget"); // 绝对路径OK //ModelAndView mav=new ModelAndView("forwardTarget"); // 相对路径也OK mav.addObject("param1", "value1"); mav.addObject("param2", "value2"); return mav ; } @RequestMapping("/forwardTarget") public String forwardTargetView(Model model, @RequestParam("param1") String param1, @RequestParam("param2") String param2) { model.addAttribute("param1", param1); model.addAttribute("param2", param2); return "forwardTarget"; } /* * redirect 目标有三种构建方式 * 1. 使用 redirect: 前缀url方式构建目标url * 2. 使用 RedirectView 类型指定目标 * 3. 使用 ModelAndView 类型指定目标, ModelAndView 视图名默认是forward, 所以对于redirect, 需要加上 redirect: 前缀 * */ @RequestMapping("/noParamRedirect") public RedirectView noParamTest() { RedirectView redirectTarget = new RedirectView(); redirectTarget.setContextRelative(true); redirectTarget.setUrl("noParamTarget"); return redirectTarget; } @RequestMapping("/noParamTarget") public String redirectTarget() { return "noParamTarget"; } @RequestMapping("/withParamRedirect") public RedirectView withParamRedirect(RedirectAttributes redirectAttributes) { RedirectView redirectTarget = new RedirectView(); redirectTarget.setContextRelative(true); redirectTarget.setUrl("withParamTarget"); redirectAttributes.addAttribute("param1", "value1"); redirectAttributes.addAttribute("param2", "value2"); return redirectTarget; } @RequestMapping("/withParamTarget") public String withParamTarget(Model model, @RequestParam("param1") String param1, @RequestParam("param2") String param2) { model.addAttribute("param1", param1); model.addAttribute("param2", param2); return "withParamTarget"; } @RequestMapping("/withFlashRedirect") public RedirectView withFlashTest(RedirectAttributes redirectAttributes) { RedirectView redirectTarget = new RedirectView(); redirectTarget.setContextRelative(true); redirectTarget.setUrl("withFlashTarget"); redirectAttributes.addAttribute("param", "value"); redirectAttributes.addFlashAttribute("flashParam", "flashValue"); return redirectTarget; } /* * redirectAttributes.addAttribute加的attr, 使用 @RequestParam()来fetch * redirectAttributes.addFlashAttribute()加的attr, 使用 @ModelAttribute()来fetch * */ @RequestMapping("/withFlashTarget") public String withFlashTarget(Model model, @RequestParam("param") String param, @ModelAttribute("flashParam") String flashParam) { model.addAttribute("param", param); model.addAttribute("flashParam", flashParam); return "withFlashTarget"; } @GetMapping("/input") public String input() { return "input"; } /* * form 提交后, 如果form数据有问题, 使用redirectAttributes.addFlashAttribute()加上 flash message. * addFlashAttribute()可以是任意类型的数据(不局限在String等基本类型) * addFlashAttribute() 加的 attr, 不会出现在url 地址栏上. * addFlashAttribute() 加的 attr, 一旦fetch后, 就会自动清空, 非常适合 form 提交后 feedback Message. * */ @PostMapping("/submit") public RedirectView submit(RedirectAttributes redirectAttributes) { boolean passed = false; if (passed==false) { RedirectView redirectTarget = new RedirectView(); redirectTarget.setContextRelative(true); redirectTarget.setUrl("input"); redirectAttributes.addFlashAttribute("errorMessage", "some error information here"); return redirectTarget; }else { RedirectView redirectTarget = new RedirectView(); redirectTarget.setContextRelative(true); redirectTarget.setUrl("inputOK"); return redirectTarget; } } }
==============================
Html 代码
==============================
-------------------------------
input.html
-------------------------------
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title> </title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous"> <link href="https://getbootstrap.com/docs/4.0/examples/signin/signin.css" rel="stylesheet" crossorigin="anonymous"/> </head> <body> <div class="container"> <form class="form-signin" method="post" action="/submit"> <h2 class="form-signin-heading">Please input</h2> {% if errorMessage is not empty %} <div class="alert alert-danger" role="alert">{{errorMessage}}</div> {% endif %} <p> <label for="username" class="sr-only">Username</label> <input type="text" id="username" name="username" class="form-control" placeholder="Username" required autofocus> </p> <button class="btn btn-lg btn-primary btn-block" type="submit">submit</button> </form> </div > </body> </html>
-------------------------------
inputOK.html
-------------------------------
<!DOCTYPE html> <html lang="en"> <head> <title> </title> </head> <body> <div class="container"> <h1>inputOK</h1> </div > </body> </html>
-------------------------------
forwardTarget.html
-------------------------------
<!DOCTYPE html> <html lang="en"> <head> <title>{{param1}} {{param2}} </title> </head> <body> <h1>forwardTarget</h1> </body> </html>
-------------------------------
withParamTarget.html
-------------------------------
<!DOCTYPE html> <html lang="en"> <head> <title>{{param1}} {{param2}} </title> </head> <body> <h1>withParamTarget</h1> </body> </html>
-------------------------------
noParamTarget.html
-------------------------------
<!DOCTYPE html> <html lang="en"> <head> <title>{{systemName}} {{version}} </title> </head> <body> <h1>noParamTarget</h1> </body> </html>
-------------------------------
withFlashTarget.html
-------------------------------
<!DOCTYPE html> <html lang="en"> <head> <title>{{param}} {{flashParam}} </title> </head> <body> <h1>withFlashTarget</h1> </body> </html>
==============================
效果截图
==============================
转发后的url还是原始请求url, http://www.localhost:8080/forwardTest1
重定向的地址会发生改变, 请求地址 http://localhost:8080/withParamRedirect , 结果地址:
form提交验证的示例: http://localhost:8080/input
开始时的GET 请求截图:
form 提交后的截图:
转发和重定向
重定向和转发有一个重要的不同:当使用转发时,JSP容器将使用一个内部的方法来调用目标页面,新的页面继续处理同一个请求,而浏览器将不会知道这个过程。 与之相反,重定向方式的含义是第一个页面通知浏览器发送一... 查看详情
转发和重定向的区别
重定向和转发有一个重要的不同:当使用转发时,JSP容器将使用一个内部的方法来调用目标页面,新的页面继续处理同一个请求,而浏览器将不会知道这个过程。与之相反,重定向方式的含义是第一个页面通知浏览器发送一个新... 查看详情
jsp中转发和重定向的区别!
...如果A请求B,之后A请求C的时候A到B的请求断开!转发和重定向的区别!01.转发request001.转发是服务器端的行为002.数据不会丢失003.url不会发生变化,永远是第一次请求的url004.每转发一次,作用域延迟一次请求!02.重定向response001.... 查看详情
转发(forward)与重定向(redirect)
...转发页面和转发到的页面可以共享request里面的数据。重定向(redirect)是服务器根据处理逻辑,返回一个302状态码和新请求地址,告诉浏览器重新去请求这个url,这时浏览器的地址栏就会变成新的URL。因为是浏览器重新请求目标... 查看详情
转发和重定向的区别
...执行,所以必须在同一台服务器上。转发速度快。重定向:两次请求。地址栏的url会随之改变为定向后的那个url。在客户端执行,所以可以在不同的服务器上。重定向速度慢。转发:request.getRequestDispatcher("succes... 查看详情
转发和重定向的区别
转发和重定向的区别:1、转发可以共享数据,重定向不可以共享数据2、转发url地址不发生变化,重定向发生变化3、转发发送在服务器端,重定向可以客户端本文出自“yinbin”博客,请务必保留此出处http://yinbin99.blog.51cto.com/11392... 查看详情
转发和重定向
...的URL不会改变;response.sendRedirect("p3.jsp");这种方法称为重定向,地址栏的URL会改变;这样实现跳转到p3.jsp;可是这两种方法有着本质 查看详情
转发与重定向
重定向和转发有一个重要的不同:当使用转发时,JSP容器将使用一个内部的方法来调用目标页面,新的页面继续处理同一个请求,而浏览器将不会知道这个过程。与之相反,重定向方式的含义是第一个页面通知浏览器发送一个新... 查看详情
请求转发和重定向区别
1,请求重定向:客户端行为,response.sendRedirect(),从本质上讲等同于两次请求,前一次的请求对象不会保持,地址栏的URL地址会改变。 2,请求转发:服务器行为,request.getRequsetDispatcher().forward(requset,response);是一次请求,转发... 查看详情
如何使用转发 URL 执行 302 重定向
】如何使用转发URL执行302重定向【英文标题】:Howtoperforma302RedirectwithaforwardingURL【发布时间】:2021-06-2717:06:02【问题描述】:我们使用Xamarin和UITest来为我们的应用程序创建UI测试。我们正在使用AzureAPI管理进行集成,并试图模拟... 查看详情
如何从春季启动重定向/转发角度页面?
...发角度页面?【英文标题】:Howtoredirect/forwardangularpagefromspringboot?【发布时间】:2019-06-0119:53:17【问题描述】:我正在开发一个应用程序,其中Angular6作为前端,springboot作为后端。在实现用户身份验证模块时,我想在登录后相应... 查看详情
javaweb中请求转发和请求重定向的区别
针对于JavaWeb中请求与重定向的一个cheatsheep:1.转发1)完成一次转发,用户浏览器发送一次请求2)转发之后,浏览器URL地址栏不改变(服务器帮忙完成)3)请求域中数据不丢失4)服务器行为5)当前应用范围内2.重定向1)完成一... 查看详情
转发和重定向(代码片段)
重定向//访问当前页面。5秒后跳转到指定页面 response.setHeader("refresh","5;url=/WEB_servlet/servlet2");5秒之后跳转到另一个地址转发获得请求转发器---path是转发的地址RequestDispatchergetRequestDispatcher(Stringpath)通过转发器对象转发requestDispathce... 查看详情
转发(forward)和重定向(redirect)的区别
转发与重定向的主要区别转发重定向转发是服务器行为重定向是客户端行为转发浏览器url不改变重定向浏览器url改变转发request请求数据不丢失重定向request请求数据丢失转发效率较高重定向效率较低 工作流程:转发:客... 查看详情
servlet请求转发与重定向的区别
...我们可以理解为客户端请求调用服务器的资源。 3.重定向(redirect)rosponse.sendRedirect("http://www.baidu.com");跳转到指定url资源,地址栏url不同,请求响应不同(另起一个响应) 请求转发(包含)和重定向的区别1.请求转发只有一... 查看详情
请求转发与请求重定向
...象和response对象,他们属于同一个访问请求和响应。2、重定向(redirect)重定向不仅可以指定到一个web应用,还能够制定到任何JSP资源。重定向的访问结束后,浏览器的地址栏中显示URL变化。重定向的调用者与被调用者使 查看详情
php后台转发和重定向的区别及kohana框架当前url加参数方式
1、重定向是浏览器行为,浏览器地址有变化;转发是后台服务器完成,url地址不变化。2、kohana获取URL当前url是http://soyoung.kohana.com/blog/add?id=3 var_dump(Url::site(‘blog/add‘,NULL,FALSE)); 打印string‘/ 查看详情
jsp如何实现网页重定向
重定向和转发有一个重要的不同:当使用转发时,JSP容器将使用一个内部的方法来调用目标页面,新的页面继续处理同一个请求,而浏览器将不会知道这个过程。与之相反,重定向方式的含义是第一个页面通知浏览器发送一个新... 查看详情