BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Spring Mobile 1.0 Released

Spring Mobile 1.0 Released

Leia em Português

This item in japanese

SpringSource has released Spring Mobile 1.0. Spring Mobile contains extensions to Spring MVC that helps with the development of mobile web applications.

Spring Mobile takes the server side approach to mobile web development, detecting the device on the server and giving developers the ability to serve different content based on the device. This is in contrast with the use of CSS3 Media Queries and Responsive Web Design techniques, where detection and progressive enhancements are applied on the client-side.

Spring Mobile has the following 3 main features.

Device Resolution

Device Resolution is the process of analyzing the HTTP request to determine the device that originated the request. This is typically achieved by examining the User-Agent header and other request headers. It is useful when there is a need to handle mobile and desktop browsers differently. To enable, add the DeviceResolverHandlerInterceptor in the DispatcherServlet configuration.

<mvc:interceptors>
	<bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
</mvc:interceptors>

The LiteDeviceResolver is used by default for device resolution, which is based on the detection algorithm of the WordPress Mobile Pack. You may plug-in another DeviceResolver implementation by injecting a DeviceResolverHandlerInterceptor constructor argument. More sophisticated device resolvers like WURFL are capable of identifying specific device capabilities, such as screen size, manufacturer, model, or preferred markup.

You can get the current device by calling DeviceUtils.getCurrentDevice(). You can also pass the current Device as an argument to one of your @Controller methods after configuring the DeviceWebArgumentResolver.

<mvc:annotation-driven>
	<mvc:argument-resolvers>
		<bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
	</mvc:argument-resolvers>
</mvc:annotation-driven>

Site Preference Management

Site preference management allows the user to indicate if he or she prefers the mobile site or the normal site. The Spring Mobile implementation is in StandardSitePreferenceHandler, which supports query-parameter-based site preference indication and pluggable SitePreference storage. If no SitePreference has been explicitly indicated by the user, a default will be derived based on the user's device. Indicated site preferences are stored in a SitePreferenceRepository, and the default implementation stores the preference in a client-side cookie.

To enable Site Preference Management, add the SitePreferenceHandlerInterceptor to the DispatcherServlet configuration.

<mvc:interceptors>
	<bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
	<bean class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />
</mvc:interceptors>

The user may indicate the site preference by clicking on a link that submits the site_preference query parameter.

<a href="?site_preference=normal">Normal</a> | <a href="?site_preference=mobile">Mobile</a>

The indicated SitePreference is available as a request attribute named "currentSitePreference". You can also pass the current SitePreference as an argument to one of your @Controller methods after configuring the SitePreferenceWebArgumentResolver.

<mvc:annotation-driven>
	<mvc:argument-resolvers>
		<bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
		<bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
	</mvc:argument-resolvers>
</mvc:annotation-driven>

Site Switching

Site Switching is the ability to switch the user to the most appropriate site based on the device and indicated site preference. There are currently 3 SiteSwitcher implementations. The "mDot" factory method constructs a SiteSwitcher that redirects mobile users to m.${DOMAIN}. The "dotMobi" factory method constructs a SiteSwitcher that redirects mobile users to ${DOMAIN - TLD}.mobi. The "urlPath" factory method constructs a SiteSwitcher that redirects mobile users to ${DOMAIN}/${MOBILE_PATH}. Here's an example of how to configure the mDot SiteSwitcher.

<mvc:interceptors>
	<bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
	<bean class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor" factory-method="mDot">
		<constructor-arg value="domain.com" />
	</bean>
</mvc:interceptors>

For more information, watch the 4 minute Spring Mobile screencast and read the Spring Mobile Reference Manual. To get started, download the release distribution or add the Maven dependency.

<dependency>
	<groupId>org.springframework.mobile</groupId>
	<artifactId>spring-mobile-device</artifactId>
	<version>1.0.0.RELEASE</version>
</dependency>

Spring Mobile samples can be found on GitHub.

Rate this Article

Adoption
Style

BT