Announcement Announcement Module
No announcement yet.
Content negotiation Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Content negotiation

    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?

  • #2
    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. contains an example which uses a different view based on a URL extension. This can be easily modified to instead base the view on a HTTP header.

    Example handler mapping:

    <bean id="mapping" class="org...SimpleUrlHandlerMapping">
    <property name="interceptors">
    <ref bean="viewSelectorInterceptor"/>
    <property name="mappings">