SpringMVC jumps from Controller to another Controller

1. Requirement background
Requirement : jumping between controllers in the spring MVC framework requires redirection. There are several cases: jump without parameters, jump with parameters in the form of splicing url, jump with parameters without splicing parameters, and the page can also be displayed.
I thought it was a simple thing, and I personally think that one of the more commonly used methods, Baidu has all of them, these are not problems at all, but Yi Baidu unexpectedly exceeded my expectations, and a bunch of them were not the results I wanted. I'm helpless, I'll write a comparison myself for everyone to Baidu, hahaha. . . It's not all of these writings that motivated me to write this blog.
2. Solutions There are definitely solutions to the
needs Solving them one by one shows that there are many ways to jump in spring. Here I just talk about some of the classes and methods that I think are easy to use, commonly used, and spring subpackages.
(1) I jump from one controller to another controller in the background. Why is there such a demand? It's like this. I have a list page, and then I will add a new operation. After the addition is completed in the background, I will jump to the list page without passing parameters. The list page queries all by default.
Method 1: Use ModelAndView
return new ModelAndView(“redirect:/toList”);
This can redirect to the method toList
Method 2: Return String
return “redirect:/ toList “;
Other methods: There are many other methods, not here anymore Do the introduction, such as response and so on. This is a redirect without parameters.
(2) In the second case, the list page has query conditions. After the jump, my query conditions cannot be lost, so I need to have parameters. With parameters, the url can be spliced
. Method 1: splicing the url manually
new ModelAndView(“redirect:/toList?param1=”+value1+”¶m2=”+value2);
This has the disadvantage that there may be garbled characters in Chinese transmission.
Method 2: Use RedirectAttributes, which is a relatively easy-to-use class that I found
here. use its addAttribute method here. This actually redirects you to see the url, and it automatically spells your url for you.
Usage:
attr.addAttribute("param", value);
return "redirect:/namespace/toController";
In this way, in the toController method, the parameter can be obtained by obtaining the parameter, and then passed to the page. The url in the past is still the same as method one.
(3) You can get the value without splicing the url page with parameters (the point is this)
Generally, I guess I want to use this method when redirecting to:
@RequestMapping("/save")
public String save(@ModelAttribute("form" ) Bean form, RedirectAttributes attr)
throws Exception {

    String code =  service.save(form);  
    if(code.equals("000")){  
        attr.addFlashAttribute("name", form.getName());    
        attr.addFlashAttribute("success", "添加成功!");  
        return "redirect:/index";  
    }else{  
        attr.addAttribute("projectName", form.getProjectName());    
        attr.addAttribute("enviroment", form.getEnviroment());    
        attr.addFlashAttribute("msg", "添加出错!错误码为:"+rsp.getCode().getCode()+",错误为:"+rsp.getCode().getName());  
        return "redirect:/maintenance/toAddConfigCenter";  
    }  
}  

@RequestMapping(“/index”)

public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr)  
               throws Exception {  
        return "redirect:/main/list";  
}  

I don't need to say the page value, it can be obtained directly with the el expression. The principle here is to put it in the session, and the session will remove the object immediately after jumping to the page. So this value will be lost after you refresh it.
3. Summary There are still two kinds of jumps at
the bottom, but spring has been encapsulated again, so there are actually many, many ways to jump. You can seal one yourself, or you can use the most primitive response, no problem. . Alright, here we go.
In fact, it's nothing, but it's very simple to know this. I didn't understand it before, but now I understand it and share it with you. Message me if you have any questions.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324658671&siteId=291194637