Reputation: 229
How can we use spring RedirectAttributes
to add a attribute with same name and multiple values, like if I have HTTP request parameter say place=london&place=paris
, and I want to redirect these "place" params in a redirectAttribute
.
redirectAttribute.addAttribute("place",places??)
I don't want to use flash attributes.
Is it possible?
Upvotes: 1
Views: 1599
Reputation: 81
You can use the mergeAttributes method:
String[] attrArray = {"london", "paris"};
Map<String, String[]> attrMap = new HashMap<>();
attrMap.put("place", attrArray);
redirectAttrs.mergeAttributes(attrMap);
This will create a URL with request parameters like this: place=london%2Cparis
(where %2C
is the ASCII keycode in hexadecimal for a comma), which is equivalent to place=london&place=paris
.
Upvotes: 0
Reputation: 61
I'm really frustrated that there doesn't appear to be a cleaner solution, but I faced the same problem and ended up doing:
((HashMap<String, Object>) redirectAttrs).putIfAbsent("place",
Arrays.asList("london", "paris"));
This works because it bypasses the way that RedirectAttributes normally converts everything to a String when you add it, since they forgot to override the putIfAbsent
method...
It works (for now), but I certainly don't recommend it.
Upvotes: 1