Jump between controller s in spring boot tutorial

As shown below, create two controllers to test the jump effect. All of the following jump modes take these two controllers as examples.

♛ 1 sendRedirect mode
response.sendRedirect("redirect: mapping path? Parameter name =" + parameter value);
♛ 2 forward mode
package com.demo.controller.Jump;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by toutou on 2019/1/27.
 */
@Controller
@Slf4j
public class FirstJumpController {
    @RequestMapping(value = "/myfirst")
    public String MyFirst(){
        // If used ModelAndView mode: return new ModelAndView("forward:/mysecond");
        // forward It is a request forwarding and a server-side behavior, which is equivalent to a request in the address bar URL It won't change.
        return "forward:/mysecond";
    }
}
♛ 3 redirect mode
    @RequestMapping(value = "/myfirst2")
    public String MyFirst2(){
        // redirect It's a request redirection and a client behavior. It's equivalent to two requests in the address bar URL Will change.
        return "redirect:/mysecond";
    }
♛ 4 splicing url mode
    @RequestMapping(value = "/myfirst3")
    public String MyFirst3(String name){
        // redirect It's a request redirection and a client behavior. It's equivalent to two requests in the address bar URL Will change.
        return "redirect:/mysecond2?name=" + name;
    }

bug: if the parameter contains Chinese characters, it is easy to cause problems

♛ 5 RedirectAttributes method

The first is: redirectattributes. AddAttribute ("prama", value);

redirectAttributes.addAttribute("prama1",value1);
redirectAttributes.addAttribute("prama2",value2);
return:"redirect: /path/list" 

Insufficient: this method is equivalent to appending the passed parameter to the redirect link address: return:"redirect: / path/list"? Prama1 = value1 & prama2 = Value2 (directly appending parameters exposes the passed parameters to the address of the link, which is very unsafe)

The second is: redirectAttributes.addFlashAttribute("prama",value);

redirectAttributes.addFlashAttribute("prama1",str);
redirectAttributes.addFlashAttribute("prama2",list);
redirectAttributes.addFlashAttribute("prama3",map);
return:"redirect: /path/list" ;

This method hides the parameters and does not directly expose the link address, but can and can only obtain the value of prama on the redirected page. The principle is that the parameters are put into the session, and the session removes the object immediately after the jump. If you redirect to a controller, you cannot get the value of the prama.

♛ 6 modelAndView mode
public ModelAndView findProjectPage() {
        ModelAndView modelAndView = new ModelAndView("redirect:Mapping path?Parameter name="+Parameter value);
        XXXXXXX
        return modelAndView;
    }

v source address

https://github.com/toutouge/javademosecond/tree/master/hellospringboot


Author: Please call me chief brother
Exit: http://www.cnblogs.com/toutou/
About the author: focus on the project development of the basic platform. If you have any questions or suggestions, please let me know!
Copyright notice: the copyright of this article belongs to the author and blog park. You are welcome to reprint it. However, without the consent of the author, you must keep this notice and give the original link in the obvious position of the article page.
It is hereby declared that all comments and private letters will be replied in the first time. We also welcome the garden's big people to correct their mistakes and make common progress. perhaps Direct personal trust I
Support blogger: if you think the article is helpful to you, you can click the bottom right corner of the article[Recommend A bit. Your encouragement is the most motive force for the author to persist in original and continuous writing!

Keywords: Java Session Lombok github

Added by aniket_dj on Sat, 14 Mar 2020 14:44:22 +0200