Spring Mobile移动端访问后台服务设备类型检测、网站偏好及分辨率感知

目录

Spring Boot配置

设备检测

网站偏好

设备感知的视图分辨率


Spring Mobile是一个框架,该框架提供的功能可以检测向您的Spring网站发出请求的设备的类型,并基于该设备提供其他视图。像所有Spring项目一样,Spring Mobile的真正强大之处在于它的扩展容易程度。

特征

  • 设备解析器抽象,用于移动和平板设备的服务器端检测

  • 网站首选项管理,允许用户指示他或她是否喜欢“正常”,“移动”或“平板电脑”体验

  • 站点切换器,能够根据用户的设备和显示的站点首选项,将用户切换到最适合的网站,无论是手机,平板电脑还是普通网站

  • 设备感知视图管理,用于组织和管理特定设备的不同视图

Spring Boot配置

要使用Spring Mobile,请将以下依赖项添加到您的应用程序中

Maven
<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>
Gradle
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;
}
©️2020 CSDN 皮肤主题: 护眼 设计师: 闪电赇 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值