Jump between controllers

Time:2020-9-14

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

请叫我头头哥_SpringBoot()controller 之间实现跳转

♛ 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 modelandview mode is used: return new modelandview ("forward / mysecond");
        //Forward refers to request forwarding and server-side behavior. It is equivalent to a request, and the URL of the address bar will not change.
        return "forward:/mysecond";
    }
}
♛ 3 redirect mode
@RequestMapping(value = "/myfirst2")
    public String MyFirst2(){
        //Redirect is the request redirection, which is the client behavior. It is equivalent to two requests, and the URL of the address bar will change.
        return "redirect:/mysecond";
    }
♛ 4 splicing URL mode
@RequestMapping(value = "/myfirst3")
    public String MyFirst3(String name){
        //Redirect is the request redirection, which is the client behavior. It is equivalent to two requests, and the URL of the address bar will change.
        return "redirect:/mysecond2?name=" + name;
    }

Bug: if the parameter contains Chinese characters, it is easy to have problems

♛ 5 redirectattributes mode

First: redirectAttributes.addAttributie (“prama”,value);

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

Disadvantages: this method is equivalent to appending the parameters passed in the redirection link address:return:"redirect:/path/list?prama1=value1&prama2=value2(appending parameters directly will expose the passed parameters to the address of the link, which is very unsafe)

The second type: 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 get the prama value on the redirected page. The principle is that the parameters are placed in thesessionMedium,sessionRemove the object immediately after the jump. If you redirect to acontrollerThe prama value cannot be obtained.

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

vSource code address

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

By:Please call me boss

Origin: 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 give me your advice!
Copyright notice: the copyright of this article belongs to the author and the blog Garden. We welcome to reprint it, but we must keep this notice without the author’s consent, and give the original link in the obvious position of the article page.
I hereby declare that all comments and personal messages will be answered as soon as possible. We are also welcome to correct the mistakes and make common progress. Or write to me directly
Support blogger: if you feel that the article is helpful to you, you can click on the bottom right corner of the article[recommended]once. Your encouragement is the biggest motive force for the author to insist on originality and continuous writing!