Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
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...