This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Content negotiation

Jul 7th, 2005, 11:15 AM

What is the recommended way to handle device-dependent presentations within Spring MVC?
I now handle this by returning different ModelAndView objects for the different devices (in the relevant controllers), e.g. a ModelAndView("index-wap",..) or a ModelAndView("index-html",...). Ugly, and very limited.

Ideally, I'd like to use a ViewResolver, but the ViewResolver interface only allows me to switch views based on locales - I'd like to switch based on (for example) the contents of (parts of) user-agents headers, or other values of the HTTPaccept headers.

Seems to me that the current ViewResolver is a specific implementation of a more generic problem. Is there another way I'm currently missing?

You can write a HandlerInterceptorAdapter which is wired up by specifying an "interceptors" property for an AbstractHandlerMapping object. The interceptor can then modify the ModelAndView to for example use a different view name.