Spring Mobile Real Time Application
Spring Mobile Real Time Application
1.Introduction
Spring community provide another project Spring Mobile as an extensions to Spring MVC for
developing mobile web applications. This module provide the server side device detection and
provide site preferences to open website according to device resolution.
2.Maven Dependency
For getting this project following is maven dependency to be added to pom file.
<dependency>
<groupId>org.springframework.mobile</groupId>
<artifactId>spring-mobile-device</artifactId>
<version>${org.springframework.mobile-version}</version>
</dependency>
3. Device Module
Spring Mobile provide extension to Spring MVC for device detection. It is useful when any
request by mobile deices need to handled differently from requests made by desktop
browsers. This supported provided by Spring Module with the help of Device Resolver in the
framework. Let see how device resolver works.
4. Device Resolution Framework
This device resolution module is a framework which has collection of interfaces, filters, handler
classes and enum to working as device resolver. Lets have look following classes in this module.
4.1 DeviceResolver interface
In Spring Mobile, the DeviceResolver interface defines the API for device resolution:
boolean isNormal();
boolean isMobile();
boolean isTablet();
DevicePlatform getDevicePlatform();
Device resolution is nothing but it is the simple process of introspecting an HTTP request to
determine the device that originated the request by analyzing the User-Agent header and other
request headers.
In Spring MVC, web applications perform device resolution at the beginning before any request
handler is invoked. Then by the help of Device resolver implementation request handlers can
obtain the Device instance and by using of it request served differently as desktop request.
<interceptors>
<!-- On pre-handle, resolve the device that originated the web request
-->
<bean
class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor"
/>
</interceptors>
Java Configuration
@Bean
@Override
registry.addInterceptor(deviceResolverHandlerInterceptor());
<filter>
<filter-name>deviceResolverRequestFilter</filter-name>
<filter-
class>org.springframework.mobile.device.DeviceResolverRequestFilter</
filter-class>
</filter>
4.5 DeviceHandlerMethodArgumentResolver
Find the Current Device
There is an Untility class in the Spring Mobile to fetching current device to generate the request.
<annotation-driven>
<argument-resolvers>
<bean
class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
</argument-resolvers>
</annotation-driven>
@Bean
public DeviceHandlerMethodArgumentResolver
deviceHandlerMethodArgumentResolver() {
@Override
argumentResolvers.add(deviceHandlerMethodArgumentResolver());
You can then inject the Device into your @Controllers as shown below:
@Controller
public class HomeController {
@RequestMapping("/")
if (device.isMobile()) {
} else if (device.isTablet()) {
} else {
Above interface has one method handleSitePreference with request and response parameters
to decide SitePreference, it is actually an enum.
//...
5.3 SitePreferenceHandlerInterceptor
To enable SitePreference management before requests are processed, add the
SitePreferenceHandlerInterceptor to your DispatcherServlet configuration:
<interceptors>
</interceptors>
@Bean
@Override
registry.addInterceptor(sitePreferenceHandlerInterceptor());
SitePreference sitePreference =
SitePreferenceUtils.getCurrentSitePreference(servletRequest);
You could pass this site preference as argument with following configuration.
To configure a SitePreferenceWebArgumentResolver:
<annotation-driven>
<argument-resolvers>
<bean
class="org.springframework.mobile.device.site.SitePreferenceWebArgumentRes
olver" />
</argument-resolvers>
</annotation-driven>
@Bean
public SitePreferenceHandlerMethodArgumentResolver
sitePreferenceHandlerMethodArgumentResolver() {
@Override
argumentResolvers.add(sitePreferenceHandlerMethodArgumentResolver());
You can then inject the indicated SitePreference into your @Controller as shown below:
@Controller
if (sitePreference == SitePreference.NORMAL) {
return "home";
return "home-mobile";
return "home-tablet";
} else {
return "home";
6. Site Switching
Spring mobile also provides different types of site switchers (like mDot, dotMobi and urlPath
SiteSwitcher) which automatically redirect users to the device specific site based on the device
generating the request and site preference set by the user.
SiteSwitcherHandlerInterceptor redirect users to device specific site. There are different types
of site switchers available like mDot, dotMobi and urlPath SiteSwitcher. Bute here we have used
urlPath SiteSwitcher which redirects users to different paths within the application based on
the device and site preference.
<interceptors>
<!-- On pre-handle, resolve the device that originated the web request
-->
<bean
class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor"
/>
<bean
class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInter
ceptor"
factory-method="mDot">
</bean>
</interceptors>
@Bean
@Bean
public SiteSwitcherHandlerInterceptor siteSwitcherHandlerInterceptor() {
@Override
registry.addInterceptor(deviceResolverHandlerInterceptor());
registry.addInterceptor(siteSwitcherHandlerInterceptor());
XML configuration:
<bean
class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewReso
lver">
<constructor-arg>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</constructor-arg>
</bean>
Java-based configuration:
@Bean
delegate.setPrefix("/WEB-INF/views/");
delegate.setSuffix(".jsp");
resolver.setMobilePrefix("mobile/");
resolver.setTabletPrefix("tablet/");
return resolver;
...
...
</bean>
Java-based configuration:
@Bean
...
resolver.setEnableFallback(true);
...
return resolver;