51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Spring Boot集成Spring Mobile快速入门Demo

1.什么是Spring Mobile?

Spring Mobile是一个基于Spring Web MVC框架扩展的一个针对不同移动终端的应用开发框架。通过它我们在适配不同终端方面,就不用费劲心思了。

Spring Mobile的主要功能

  • 自动设备检测: Spring Mobile在 server端内置了一个设备解析器的抽象层。它会分析所有过来的请求,然后侦测到设备信息,比如,设备的类型,操作系统等等。

  • 网站偏好管理:使用网站偏好管理,Spring Mobile允许用户选择移动/平板电脑/网站的视图。 这是比较不赞成的技术,因为通过使用DeviceDelegatingViewresolver,我们可以根据设备类型跳转到对应的视图层,而不需要来自用户端的任何输入。

  • 站点切换器:站点切换器能够根据用户的设备类型(比如:手机,平板,浏览器等等)将用户自动切换到最合适的视图。

  • 设备感知视图管理器:通常,根据设备类型,我们将用户请求转发到特定站点,以处理特定设备。 Spring Mobile的View Manager使开发人员能够灵活地将所有视图以预定义的格式显示出来,Spring Mobile将根据设备类型自动管理不同的视图。

2.代码工程

实验目的

实验客户端类型识别

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <parent>        <artifactId>springboot-demo</artifactId>        <groupId>com.et</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>
    <artifactId>SpringMobile</artifactId>
    <properties>        <maven.compiler.source>8</maven.compiler.source>        <maven.compiler.target>8</maven.compiler.target>        <spring-mobile-device.version>1.1.5.RELEASE</spring-mobile-device.version>    </properties>    <dependencies>
        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>
        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-autoconfigure</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework.mobile</groupId>            <artifactId>spring-mobile-device</artifactId>            <version>${spring-mobile-device.version}</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-freemarker</artifactId>        </dependency>
    </dependencies></project>

controller

判断客户端来源 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

package com.et.springmobile.controller;
import java.util.logging.Logger;
import org.springframework.mobile.device.Device;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic class IndexController {
   private final static Logger LOGGER = Logger.getLogger(IndexController.class.getName());
   @GetMapping("/")   public String greeting(Device device) {
      String deviceType = "browser";      String platform = "browser";      String viewName = "index";
      if (device.isNormal()) {         deviceType = "browser";      } else if (device.isMobile()) {         deviceType = "mobile";         viewName = "mobile/index";      } else if (device.isTablet()) {         deviceType = "tablet";         viewName = "tablet/index";      }
      platform = device.getDevicePlatform().name();
      if (platform.equalsIgnoreCase("UNKNOWN")) {         platform = "browser";      }
      LOGGER.info("Client Device Type: " + deviceType + ", Platform: " + platform);
      return viewName;   }
}

config

Spring Boot自动注入了3个类,DeviceResolverHandlerInterceptor,SitePreferenceHandlerInterceptor和SitePreferenceMethodArgumentResolver。DeviceResolverHandlerInterceptor是HandlerInterceptor的一个实现,从名字来看,它拦截到应用的请求,判断发送请求设备的类型。当设备解决以后,SitePreferenceMethodArgumentResolver允许SpringMVC在Controller中使用SitePreference实体。在内部,DeviceResolverHandlerInterceptor判断请求头中的User-Agent,基于请求头中的值,判断请求是否来自浏览器(桌面)、手机、还是Pad。 SitePreferenceHandlerInterceptor利用探测到的设备,判断用户的初始站点偏好。如果用户喜欢另一个站点,则选择该站点,并在随后的请求中使用,以覆盖已解析的设备值。站点偏好是通过请求中特殊的查询字符串设置的。一旦接收到,偏好将被持久化到cookie中,以供将来参考。站点偏好功能在Spring Boot中默认是打开的,可以通过上面的设置关闭它。 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

package com.et.springmobile;
import java.util.List;
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver;import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;import org.springframework.web.method.support.HandlerMethodArgumentResolver;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configurationpublic class AppConfig implements WebMvcConfigurer {
    @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());    }
}

DemoApplication.java

package com.et.springmobile;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class DemoApplication {
   public static void main(String[] args) {      SpringApplication.run(DemoApplication.class, args);   }}

application.properties

  • spring.mobile.devicedelegatingviewresolver.enabled: truespring.freemarker.template-loader-path: classpath:/templatesspring.freemarker.suffix: .ftl

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • https://github.com/Harries/springboot-demo

3.测试

启动Spring Boot应用

测试来源

访问http://127.0.0.1:8080/,使用chrome 模拟不同的客户端,console日志输出如下: * * * * * * *

21:47:42.341 [http-nio-8080-exec-1] INFO o.s.web.servlet.DispatcherServlet - Completed initialization in 40 ms21:47:42.430 [http-nio-8080-exec-1] INFO c.e.s.controller.IndexController - Client Device Type: browser, Platform: browser21:49:10.378 [http-nio-8080-exec-5] INFO c.e.s.controller.IndexController - Client Device Type: mobile, Platform: ANDROID21:49:21.414 [http-nio-8080-exec-6] INFO c.e.s.controller.IndexController - Client Device Type: mobile, Platform: ANDROID21:49:35.192 [http-nio-8080-exec-7] INFO c.e.s.controller.IndexController - Client Device Type: mobile, Platform: IOS21:49:51.647 [http-nio-8080-exec-8] INFO c.e.s.controller.IndexController - Client Device Type: mobile, Platform: IOS

4.引用

  • https://docs.spring.io/spring-mobile/docs/current/reference/htmlsingle/

  • http://www.liuhaihua.cn/archives/710783.html

赞(3)
未经允许不得转载:工具盒子 » Spring Boot集成Spring Mobile快速入门Demo