Spring Mobile 2.0.0 M3

Spring Mobile是一个框架,它提供了检测向Spring web站点发出请求的设备类型的功能,并提供基于该设备的其他视图。像所有的Spring项目一样,Spring Mobile的真正威力在于它的可扩展性。
特征
一种用于移动和平板设备服务器端检测的设备解析器抽象
站点首选项管理,允许用户指示他/她是否喜欢“正常”、“移动”或“平板”体验
一种站点切换器,能够根据用户的设备和可选的指定站点偏好,将用户切换到最合适的站点(移动、平板电脑或普通站点)
用于组织和管理特定设备的不同视图的设备感知视图管理
弹簧靴配置
要使用SpringMobile,请将以下依赖项添加到应用程序中
马文


org.springframework.mobile
spring-mobile-device
2.0.0.M3



spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone

false


Gradle

dependencies {
compile ‘org.springframework.mobile:spring-mobile-device:2.0.0.M3’
}repositories {
maven {
url ‘https://repo.spring.io/libs-milestone’
}
}

Device detection

Add the following Java-based configuration to enable device detection in a Spring web application:

@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 argumentResolvers) {
argumentResolvers.add(deviceHandlerMethodArgumentResolver());
}

Inject the Device into your controller:

@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!");
    }
}

}

Site preference

Add the following Java-based configuration to enable site preference management in a Spring web application:

@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 argumentResolvers) {
argumentResolvers.add(sitePreferenceHandlerMethodArgumentResolver());
}

Inject the SitePreference into your controller:

@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";
    }
}

}

Device aware view resolution

Add the following Java-based configuration to enable device aware views in a Spring web application. This specifies that Spring Mobile will look for mobile views in /WEB-INF/views/mobile/ and tablet views in /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;
}

Spring Initializr
Quickstart Your Project
Bootstrap your application with Spring Initializr.

你可能感兴趣的:(java)