2

I'm new to java and web apps and after trying out a few things I went with a set up of Spring webmvc using annotations and velocity as templating engine. It's not that hard to do simple @RequestMapping annotations to controller methods and returning ModelAndView instances filled with data, however I was wondering how things are done when you have data you need in the model that occurs on every page, for example "latest 5 news items" or something similar. You could of course always fill the model with such data in every method that is handled by a @RequestMapping, but I'm quite sure that that is not the way to do it.

What is the correct way of filling the model with recurring data, without poluting your controller methods with calls to the same method for this recurring data.

Any help is appreciated.

Ronner
  • 183
  • 1
  • 6

3 Answers3

1

You could use a servlet filter or a Spring interceptor, and get the recurring data from this filter or interceptor and place it in a request attribute.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Spring interceptor it is :) Is it possible to set up interceptors with annotations? Or can you set them up using xml configuration? – Ronner Apr 29 '12 at 18:33
  • I also wonder if using Aspects would provide more flexibility so you can target specific RequestMappings in controllers? Any reason you would use interceptors instead of Aspects? – Ronner Apr 29 '12 at 19:10
1

Another solution is to let the page call more than one controller, for example by using multiple ajax requests. Then one controller could is responsible for the specific page and another is responsible for "latest 5 news items", see related question.

Community
  • 1
  • 1
matsev
  • 32,104
  • 16
  • 121
  • 156
0

Good question dude. In my current app i am using sessions to store my username which is appearing on all the rest of the app.

@JB Nizet thanks for the link.. now ill go for spring interceptor

Raghuveer
  • 707
  • 7
  • 18