0

I'm having some problems with accessing org.springframework.ui.Model in a bean not annotated by @Controller Using @AutoWired fails in this bean and it isn't created. Similarly, if I try to make Model a parameter to a method in this bean it is not injected and the method call fails silently.

The reason for the need is to supply dynamic data to some views in my site such as allowed links for a user, or custom welcome messages. Theses elements may not be on every page. I've seen some tempting frameworks like Tiles. View templates were working, but needed model element on partial views was not being populated.

I've seen similar posts such as Add attributes to the model of all controllers in Spring 3 but this would add to all views which is not correct in this case. Any help or pointing in the right direction would be appreciated.

Community
  • 1
  • 1
Craig Smith
  • 66
  • 1
  • 9
  • Could you put your controller's class? – nnunes10 Feb 17 '16 at 21:25
  • it's not the controller where I have the problem. It's autowiring outside the controller I am looking for – Craig Smith Feb 17 '16 at 21:32
  • Why not just put the logic in a service class and call the service method in your controller? Then you can add whatever you need to the model based on the page called. I can post an example if that's not clear – riddle_me_this Feb 17 '16 at 22:14
  • @bphilipnyc, that makes sense and what I was doing in the beginning till I saw that every new controller I'm calling the same 3 methods and now most controllers are now dependent on these service classes. I was looking for a cleaner way using annotations or AOP. – Craig Smith Feb 19 '16 at 20:43
  • Why not do: `@RequestMapping({ "/page1", "/page2" })` on your controller method? – riddle_me_this Feb 19 '16 at 21:12
  • because while page 1 and page 2 would have common elements like links, header, and footer, the content would be different. – Craig Smith Feb 19 '16 at 22:19

0 Answers0