3

I'm currently using the @ModelAttribute annotation in my controllers to add reference data to my pages and forms, ie:

@ModelAttribute("someValue")
public String getSomeValue() {
   return someValue;
}

This works great until I start using redirects from the controllers. All of the values from methods marked with @ModelAttribute appear in the URL, ie:

http://somedomain.com/page?someValue=value

Is there a setting to turn this off? Or is there a simple fix for this?

I read something about creating an interceptor for adding reference data into a model, but that just seems wrong:

http://developingdeveloper.wordpress.com/2008/02/28/common-reference-data-in-spring-mvc/

skaffman
  • 398,947
  • 96
  • 818
  • 769
Brandon
  • 2,886
  • 3
  • 29
  • 44
  • Uncanny, someone else just asked something very similar: http://stackoverflow.com/questions/4651537/what-are-the-best-practices-around-setting-global-model-attributes-in-spring-mvc/4651760#4651760 – skaffman Jan 10 '11 at 22:18

2 Answers2

3

I found out that there is a setter on the RedirectView object called setExposeModelAttributes. If you set it to false, the attributes don't get thrown into the URL.

I got some help from PUK_999 in the spring source forums:

http://forum.springsource.org/showpost.php?p=274948&postcount=6

Brandon
  • 2,886
  • 3
  • 29
  • 44
0

This is intentional and specific behaviour of @ModelAttribute, even if it does feel wrong and broken.

An interceptor is really one of the easiest ways of doing this.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • I understand that the result is intentional, but I'm looking for a solution that will allow me to override some of the behavior. Creating an interceptor seems like quite a hack especially when you may have multiple parent controllers, all of which have different reference data. – Brandon Jan 10 '11 at 22:37