目录
Spring Mobile是一个框架,该框架提供的功能可以检测向您的Spring网站发出请求的设备的类型,并基于该设备提供其他视图。像所有Spring项目一样,Spring Mobile的真正强大之处在于它的扩展容易程度。
特征
设备解析器抽象,用于移动和平板设备的服务器端检测
网站首选项管理,允许用户指示他或她是否喜欢“正常”,“移动”或“平板电脑”体验
站点切换器,能够根据用户的设备和显示的站点首选项,将用户切换到最适合的网站,无论是手机,平板电脑还是普通网站
设备感知视图管理,用于组织和管理特定设备的不同视图
要使用Spring Mobile,请将以下依赖项添加到您的应用程序中
<dependencies>
<dependency>
<groupId>org.springframework.mobile</groupId>
<artifactId>spring-mobile-device</artifactId>
<version>2.0.0.M3</version>
</dependency>
</dependencies><repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
dependencies {
compile 'org.springframework.mobile:spring-mobile-device:2.0.0.M3'
}repositories {
maven {
url 'https://repo.spring.io/libs-milestone'
}
}
添加以下基于Java的配置以在Spring Web应用程序中启用设备检测:
@Bean
public DeviceResolverHandlerInterceptor
deviceResolverHandlerInterceptor() {
return new DeviceResolverHandlerInterceptor();
}
@Bean
public DeviceHandlerMethodArgumentResolver
deviceHandlerMethodArgumentResolver() {
return new DeviceHandlerMethodArgumentResolver();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(deviceResolverHandlerInterceptor());
}
@Override
public void addArgumentResolvers(
List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(deviceHandlerMethodArgumentResolver());
}
将Device
注入您的控制器:
@Controller
public class HomeController {
private static final Logger logger =
LoggerFactory.getLogger(HomeController.class);
@RequestMapping("/")
public void home(Device device) {
if (device.isMobile()) {
logger.info("Hello mobile user!");
} else if (device.isTablet()) {
logger.info("Hello tablet user!");
} else {
logger.info("Hello desktop user!");
}
}
}
添加以下基于Java的配置以在Spring Web应用程序中启用站点首选项管理:
@Bean
public DeviceResolverHandlerInterceptor
deviceResolverHandlerInterceptor() {
return new DeviceResolverHandlerInterceptor();
}
@Bean
public SitePreferenceHandlerInterceptor
sitePreferenceHandlerInterceptor() {
return new SitePreferenceHandlerInterceptor();
}
@Bean
public SitePreferenceHandlerMethodArgumentResolver
sitePreferenceHandlerMethodArgumentResolver() {
return new SitePreferenceHandlerMethodArgumentResolver();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(deviceResolverHandlerInterceptor());
registry.addInterceptor(sitePreferenceHandlerInterceptor());
}
@Override
public void addArgumentResolvers(
List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(sitePreferenceHandlerMethodArgumentResolver());
}
将SitePreference
注入您的控制器:
@Controller
public class HomeController {
private static final Logger logger =
LoggerFactory.getLogger(HomeController.class);
@RequestMapping("/")
public String home(SitePreference sitePreference, Model model) {
if (sitePreference == SitePreference.NORMAL) {
logger.info("Site preference is normal");
return "home";
} else if (sitePreference == SitePreference.MOBILE) {
logger.info("Site preference is mobile");
return "home-mobile";
} else if (sitePreference == SitePreference.TABLET) {
logger.info("Site preference is tablet");
return "home-tablet";
} else {
logger.info("no site preference");
return "home";
}
}
}
添加以下基于Java的配置,以在Spring Web应用程序中启用设备感知的视图。这指定Spring Mobile将在中查找移动视图,在中查找/WEB-INF/views/mobile/
平板电脑视图/WEB-INF/views/tablet/
。
@Bean
public LiteDeviceDelegatingViewResolver liteDeviceAwareViewResolver() {
InternalResourceViewResolver delegate =
new InternalResourceViewResolver();
delegate.setPrefix("/WEB-INF/views/");
delegate.setSuffix(".jsp");
LiteDeviceDelegatingViewResolver resolver =
new LiteDeviceDelegatingViewResolver(delegate);
resolver.setMobilePrefix("mobile/");
resolver.setTabletPrefix("tablet/");
return resolver;
}