View Full Version : Modify the view name that was returned by a controller
NickB
Jul 30th, 2009, 08:25 AM
Where would be the best place to intercept a response and modify the name of the view before it is rendered? Basically I want to perform a database query and depending on the results of the query I might want to prepend an identifier to the view name before delegating to the Apache Tiles 2 view resolver (org.springframework.web.servlet.view.tiles2.Tiles View).
I think that I might be able to extend the Apache Tiles 2 view resolver and override some method to do this, but I'm not quite sure which method to override such that I can get and alter the current view name.
Any ideas anyone?
Thanks
-Nick
jglynn
Jul 30th, 2009, 09:15 AM
What would be the pain of performing this in the controller while constructing the Model&View?
NickB
Jul 30th, 2009, 09:41 AM
Having to perform this logic in every request handling method in every controller in a large system. Having to remember to perform this logic in every new request handling method as the system is developed further. Having to train new developers to do the same, and having to rely on them remembering to do so. General long term maintainability of the system.
Doing this in 'every' (used loosely as not every method of every controller will handle requests) method of controller really is not an elegant solution and hence not viable because I know it can be done much more elegantly, I just have little experience with Spring so I'm looking for the opinion of more experienced users as to where would be best to implement this solution.
Thanks.
Marten Deinum
Jul 31st, 2009, 01:15 AM
Well how about writing a HandlerInterceptor, implement the postHandle method.
NickB
Jul 31st, 2009, 03:12 AM
Well how about writing a HandlerInterceptor, implement the postHandle method.
That worked great, thank you!
For the record if anyone else is interested in this, I also tried extending UrlBasedViewResolver (as opposed to TilesView) to implement this solution. I overrode the createView method as it is the first method that is called that receives a view name as a parameter. I ran into caching issues so that one user might see views intended for another user. Lets say I logged in with admin first and loaded up homePage.view, then I logged in as a normal user and my createView method returned user_homePage.view, the generated view would still be homePage.view due to the caching. However if the regular user loads up manageAccount.view (which gets translated to user_manageAccount.view) then the admin would also see user_ManageAccount.view. The HandlerInterceptor.postHandle method solves this problem by allowing the view to be updated before any view resolution is done.
I hope this information is useful to someone eventually!
Thanks again Marten.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.