Book Image

SPRING COOKBOOK

By : Jerome Jaglale, Yilmaz
Book Image

SPRING COOKBOOK

By: Jerome Jaglale, Yilmaz

Overview of this book

This book is for you if you have some experience with Java and web development (not necessarily in Java) and want to become proficient quickly with Spring.
Table of Contents (14 chapters)
13
Index

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);
    return resolver;
}

How it works…

For a controller returning the userList String, the /WEB-INF/userList.jsp JSP view will be used if the site preference is normal. The /WEB-INF/mobile/userList.jsp JSP view will be used...