1

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?

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
Chandan
  • 229
  • 1
  • 5
  • 14
  • Flash attributes are redirect attributes. Just add an array or list with two elements. – Sotirios Delimanolis Oct 08 '15 at 05:05
  • Some how I am loosing flash attributes in my redirects, some upper layer third party server code, is flushing them. so can't use flash attributes – Chandan Oct 08 '15 at 05:21
  • 1
    @Sotirios Delimanolis: redirect attributes and flash attributes are very different in the way they work. redirect-attributes are parameter of the redirect url. flash-attribtes are stored in the users session (flash-map). See http://stackoverflow.com/a/14470824/280244 – Ralph Oct 08 '15 at 05:37
  • @Ralph I've been using the terms flash attributes and redirect attributes interchangeably. I've never actually used the attributes that are passed as a query string. Thanks. – Sotirios Delimanolis Oct 08 '15 at 14:43

2 Answers2

1

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.

Cameron
  • 61
  • 1
  • 1
0

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.

Pablo Tirado
  • 71
  • 2
  • 5