英文原文:https://projects.spring.io/spring-mobile/
目录
Spring Mobile是Spring MVC的扩展,旨在简化移动Web应用程序的开发。
Spring Mobile是一个框架,它提供了检测向Spring网站发出请求的设备类型以及基于该设备提供备用视图的功能。 像所有Spring项目一样,Spring Mobile的真正强大之处在于它可以轻松扩展。
在项目中使用spring-mobile的推荐方法是使用依赖管理系统 - 下面的代码片段可以复制并粘贴到您的构建中。 需要帮忙? 请参阅我们的Maven和Gradle构建入门指南。
添加以下基于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());
}
将设备注入控制器:
@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/tablet/中的/ WEB-INF/views/mobile/和平板电脑视图中查找移动视图。
@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;
}