Hello, everyone. I'm a passer-by. This is Chapter 29 of spring MVC series.
1. The content of this paper includes three knowledge points
- How to implement forwarding in spring MVC?
- How to implement spring MVC redirection?
- Redirect three parameter transfer methods
2. Forward
2.1. servlet native forwarding
request.getRequestDispatcher(path).forward(request,response);
2.2. Spring MVC realizes forwarding
Interfaces that meet the following three requirements will be handled as forwarding by spring MVC
- The return value of the interface is of String type
- Return value format: forward: forward path
- Do not mark @ ResponseBody annotation on method or class
The case code is as follows. When accessing / forward/test1, the return value starts with forward:, and spring MVC will forward the request to / forward/test2
@RequestMapping("/forward/test1") public String test1() { return "forward:/forward/test2"; } @RequestMapping(value = "/forward/test2", produces = "text/html;charset=UTF-8") @ResponseBody public String test2() { return "I am test2"; }
Test effect: access / forward/test1 output in the browser
3. Redirect
3.1. servlet native redirection
response.sendRedirect(url);
3.2. Spring MVC realizes redirection
Interfaces that meet the following three requirements will be handled as forwarding by spring MVC
- The return value of the interface is of String type
- Return value format: redirect: redirected path
- Do not mark @ ResponseBody annotation on method or class
The case code is as follows. When accessing / redirect/test1, the return value starts with redirect:, and spring MVC will forward the request to / redirect/test2
@RequestMapping("/redirect/test1") public String test1() { return "redirect:/redirect/test2"; } @RequestMapping(value = "/redirect/test2", produces = "text/html;charset=UTF-8") @ResponseBody public String test2() { return "I am test2"; }
Test effect: visit / redirect/test1 in the browser. The effect is as follows. The browser address bar becomes / redirect/test2
3. Parameter redirection after splicing: 1
Directly splice parameters in the redirected url, such as redirect: address? name = passerby & age = 30, e.g
@RequestMapping("/redirect/test1") public String test1() { return "redirect:/redirect/test2?name=Passerby&age=30"; }
3.4. Redirect parameter transfer method 2: redirectattributes AddAttribute ("parameter", "value")
usage
- A parameter of type RedirectAttributes is required in the interface
- Call redirectattributes AddAttribute ("parameter", "value"), put the parameter to be passed in redirection
- **Principle: * * via redirectattributes For the parameters thrown in addAttribute, when spring MVC redirects, these parameters will be automatically marked with? Parameter 1 = value 1 & parameter 2 = value 2 are spliced to the redirected address, similar to method 1 above.
Case code
The access interface test3 will be redirected to test4, passing two parameters
@RequestMapping("/redirect/test3") public String test3(RedirectAttributes redirectAttributes) { redirectAttributes.addAttribute("name", "Passerby"); redirectAttributes.addAttribute("age", 30); return "redirect:/redirect/test4"; } @RequestMapping(value = "/redirect/test4", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map<String, Object> test4(@RequestParam("name") String name, @RequestParam("age") int age) { Map<String, Object> result = new LinkedHashMap<>(); result.put("name", name); result.put("age", age); return result; }
Test effect
If you access the / redirect/test3 interface in the browser, you will be redirected to / redirect/test4. The effect is as follows: the two parameters name and age lost in addAttribute in test3 method are automatically spliced behind the address.
3.5. Redirect parameter transfer method 2: redirectattributes Addflashattribute ("parameter", "value")
Above, we use addAttribute of RedirectAttributes to put in the parameters. This time, we will use another method addFlashAttribute to put in the parameters to be passed in redirection. What's the difference? Please look down.
usage
- A parameter of type RedirectAttributes is required in the interface
- Call redirectattributes Addflashattribute ("parameter", "value"), the parameters passed in this way are hidden and will not be spliced after the address. It is realized internally through session shared data.
- The interface that is redirected to needs to use an org springframework. ui. Model or org springframework. ui. Receive the passed parameters with parameters of modelmap type and call model Getattribute ("parameter name") can get the passed parameters
Case code
The access interface test5 will be redirected to test6, passing two parameters
@RequestMapping("/redirect/test5") public String test5(RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("name", "Passerby"); redirectAttributes.addFlashAttribute("age", 30); return "redirect:/redirect/test6"; } /** * You need to use an org springframework. ui. Model or org springframework. ui. The parameters of modelmap type are used to receive the passed parameters, * Method calls model Getattribute ("parameter name") can get the passed parameters * * @param model * @return */ @RequestMapping(value = "/redirect/test6", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map<String, Object> test6(Model model) { String name = (String) model.getAttribute("name"); Integer age = (Integer) model.getAttribute("age"); Map<String, Object> result = new LinkedHashMap<>(); result.put("name", name); result.put("age", age); return result; }
Test effect
If you access the / redirect/test5 interface in the browser, you will be redirected to / redirect/test6. The effect is as follows: the parameters are passed successfully, and the transfer is hidden.
principle
redirectAttributes.addFlashAttribute puts the parameters that need to be passed in the redirection. Before redirecting to the new address, spring MVC will throw this part of data into the session. When the redirection request comes, spring MVC will get this part of data from the session, then throw it into the Model or ModelMap, and then flush this part of data into the session.
3.6,RedirectAttributes.addAttribute and redirectattributes Addflashattribute difference
- Can realize redirection and pass parameters
- The parameters passed by addAttribute will be appended to the new address, while the parameters passed by addFlashAttribute are hidden
- addFlashAttribute can transfer a lot of information, but addFlashAttribute has a disadvantage. After redirecting to the new address, as shown in the following figure, if the user refreshes the page at this time, the passed parameters cannot be obtained and will be lost. It is recommended to use methods 1 and 2; Mode 3 can be used as an understanding.
4. Case code git address
4.1. git address
https://gitee.com/javacode2018/springmvc-series