As Jesse mentioned we use a custom HandlerInterceptorAdapter to inject common 
objects into all responses from the controllers.  Here is a brief snippet on 
how it would work:

public class CommonModelHandlerInterceptor extends HandlerInterceptorAdapter {  
 

    /****/

    @Autowired
    public CommonModelHandlerInterceptor(UserService userService, 
PortalSettingsService portalSettingsService) {
        /****/
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse 
response, Object handler, ModelAndView modelAndView) throws Exception {      
        if (modelAndView != null) {
            ModelMap map = modelAndView.getModelMap();            
            map.addAttribute(ModelKeys.AUTHENTICATED_USER, 
userService.getCurrentAuthenticatedUser());
            map.addAttribute(ModelKeys.PORTAL_SETTINGS_MAP, 
portalSettingsService.getAllPortalSettings());
        }
       

        super.postHandle(request, response, handler, modelAndView);
    }
}

then in dispatcher-servlet.xml add:

<bean 
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="commonModelHandlerInterceptor"/>
            </list>
        </property>
</bean>            

Viola!  You've now injected any common objects into the Spring ModelAndMap 
object returned by all controllers.

---
Anthony Carlucci | SW App Dev Eng, Sr. | R501 / KW App Development & Maint
e: [email protected] | v: 781.271.2432 | f: 781.271.3299
The MITRE Corporation | 202 Burlington Rd | Bedford, MA 01730-1420

-----Original Message-----
From: Ciancetta, Jesse E. [mailto:[email protected]] 
Sent: Thursday, November 03, 2011 10:21 AM
To: [email protected]
Subject: RE: Portal preferences set by admin

I've seen Spring HandlerInterceptor's used in the past for this sort of thing 
and it's worked out pretty well:

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor

More specifically -- I've seen a class extending HandlerInterceptorAdapter 
registered with the DefaultAnnotationHandlerMapping to add common objects to 
the ModelAndView in the HandlerInterceptorAdapter.postHandle(...) method.

Tony was actually the one who implemented the specific case I'm thinking of for 
our internal portal -- maybe he could comment further if there are questions on 
it.

--Jesse

>-----Original Message-----
>From: Jasha Joachimsthal [mailto:[email protected]]
>Sent: Thursday, November 03, 2011 8:56 AM
>To: [email protected]
>Subject: Portal preferences set by admin
>
>Hi,
>
>in the design of the admin interface there's also a tab for portal wide
>preferences. The admin can change things like
>- the title suffix for each page (" - Rave" will then be replaced by " -
>ACME University")
>- the number of items on a page in the widget store (or any other pageable
>list)
>- company logo
>etc etc
>
>We could create some base controller with a "getPortalSettings" that calls
>some PortalSettingsService that retrieves settings through a repository
>from the db (for each request...). All controllers should then extend this
>base controller.
>I'm wondering what the cleanest solution would be in Spring MVC or should
>my suggestion be the good enough option for the time being?
>
>
>Jasha Joachimsthal
>
>Europe - Amsterdam - Oosteinde 11, 1017 WT Amsterdam - +31(0)20 522 4466
>US - Boston - 1 Broadway, Cambridge, MA 02142 - +1 877 414 4776 (toll free)
>
>www.onehippo.com

Reply via email to