Using different JSP views for mobiles automatically

Instead of having to manually select the correct JSP in each controller method depending on the request device or site preference, use LiteDeviceDelegatingViewResolver provided by Spring Mobile.

How to do it…

In the Spring configuration class, replace any existing ViewResolver bean with a LiteDeviceDelegatingViewResolver bean:

@Bean
public LiteDeviceDelegatingViewResolver liteDeviceAwareViewResolver() {
    InternalResourceViewResolver delegate = new InternalResourceViewResolver();
    delegate.setPrefix("/WEB-INF/jsp/");
    delegate.setSuffix(".jsp");
    LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(delegate);
    resolver.setMobilePrefix("mobile/"); resolver.setEnableFallback(true); ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.