If the URL to access and the request URL received by Spring are different, you will not be able to pass parameters to the redirect destination even with FlashAttribute! !!
For example, suppose you have a proxy that changes a request for domain / ~
to domain / hoge / ~
.
I'm sure it looks like this: kissing: (appropriate)
nginx.conf
location / {
proxy_pass http://tomcat:8080/hoge/;
}
In that, when I executed addFlashAttribute of RedirectAttributes, I encountered a situation where it was not mapped at the redirect destination. Maybe something like this: kissing_heart: (more appropriate)
HogeController.java
@RequestMapping(value="/hoge/save", method = RequestMethod.POST)
public String save(@ModelAttribute("form") HogeForm form, RedirectAttributes redirectAttributes) {
:
:
redirectAttributes.addFlashAttribute("form", form);
return "redirect:/complete";
}
@RequestMapping(value="/hoge/complete", method = RequestMethod.GET)
public String complete(@ModelAttribute("form") HogeForm form) {
log.info(form); //empty! !!
return "hoge.html";
}
You can afford FlashAttribute! The problem that occurred just before I thought. I investigated from the reverse proxy direction, but I do not know the cause at all. .. ..
:thinking: :thinking: :expressionless: :sleepy: :sleeping:
: desktop: "If RedirectAttributes doesn't work, you can use FlashMap directly."
What an outrage! Looking at the implementation method while thinking, I am calling a method called setTargetRequestPath. .. ..
: hashed: ... target ... pass?
What was happening was that the redirected path (/ complete
) and the redirected path (/ hoge / complete
) did not match, so the FlashMap values were not bound! !!
With this kind of modification, parameters can be passed to the redirect destination even if a reverse proxy is set.
AbstractController.java
protected String redirect(String path, Map<String,Object> attributeMap) {
//Internal path(@RequestMapping(value)The value of the)
String innerPath = "/hoge" + path;
//Refill to FlashMap
FlashMap flashMap = new FlashMap();
attributeMap.forEach(flashMap::put);
//Set FlashMap target to internal path
flashMap.setTargetRequestPath(innerPath);
//FlashMap set
RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashMap, request, response);
//The redirect destination is an external path(Access from a browser)
return "redirect:" + path;
}
HogeController.java
@RequestMapping(value="/hoge/save", method = RequestMethod.POST)
public String save(@ModelAttribute("form") HogeForm form) {
:
:
return redirect("/complete", Map.of("form", form));
}
Even if you are using rewrite and the internal path and external path cannot be simply replaced like this time, the principle is the same, so please do your best ** and specify the internal path and external path.
FlashMap was written in the following article in an easy-to-understand manner. I wanted to find it sooner. .. .. : innocent: Pass the value to the redirect destination without using Redirect Attributes in Spring MVC
Recommended Posts