Spring: detects the types of devices that access the website.
[This tutorial is translated from the Spring official website and deleted as appropriate.]
You will set up
Create a Spring MVC project to detect the device types (mobile phones, computers, tablets) that access the website, and dynamically switch the displayed view.
Tools
A text editor, JDK 3.0 and later, Maven 1.11 + or Gradle +. (Maven will be used in this article)
Pom. xml list:
4.0.0
org.springframework
gs-device-detection
0.1.0
org.springframework.boot
spring-boot-starter-parent
1.1.5.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.mobile
spring-mobile-device
hello.Application
maven-compiler-plugin
org.springframework.boot
spring-boot-maven-plugin
spring-releases
Spring Milestone Repository
http://repo.spring.io/libs-release
spring-releases
Spring Milestone Repository
http://repo.spring.io/libs-release
Create a project
First, create a directory structure that complies with Maven specifications, src/main/java/hello
[Plain]View plaincopy
- ── Src
- ── Main
- ── Java
- ── Hello Automatic Configuration:
Observe pom. xml and find the new Spring Mobile dependency. In this way, Spring Boot automatically configures DeviceResolverHandlerInterceptor and DeviceHandlerMethodArgumentResolver. The former (DeviceResolverHandlerInterceptor) checks the User-Agent header in the request. The header information can be used to determine whether the request is from a mobile phone, a computer, or a tablet. The latter (DeviceHandlerMethodArgumentResolver) allows Spring MVC to use parsed Device objects in a controller method.
The following is a list of controllers:
package hello;import org.springframework.mobile.device.Device;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class DeviceDetectionController { @RequestMapping("/detect-device") public @ResponseBody String detectDevice(Device device) { String deviceType = "unknown"; if (device.isNormal()) { deviceType = "normal"; } else if (device.isMobile()) { deviceType = "mobile"; } else if (device.isTablet()) { deviceType = "tablet"; } return "Hello " + deviceType + " browser!"; }}
In this example, not relying on a view technology (such as JSP) in HTML to parse data, but writing strings directly to HTTP Response. The @ ResponseBody annotation tells Spring MVC to write a returned object to the response body, instead of rendering a model to a view.
The Application class is as follows:
package hello;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.SpringApplication;import org.springframework.context.annotation.ComponentScan;@ComponentScan@EnableAutoConfigurationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
Here, the application is also run in the built-in Tomcat of Spring. @ ComponentScan tells Spring to Recursively search for classes with @ Component annotations under the hello package. This ensures that DeviceDetectionController is found and registered, because @ Controller is also a type of @ Component.@ EnableAutoConfiguration refers to the content of the current class loading path, which is switched to a reasonable default configuration. This application requires default built-in tomcat configuration (tomcat-embed-core.jar) and Spring MVC default configuration (spring-webmvc.jar ).
The method for packaging and execution is similar to that in the previous article,[Plain]View plaincopy
- Mvn clean package then,
java -jar target/gs-device-detection-0.1.0.jar
In case of any exceptions, the console displays:
Computer Browser access:
The IP address of the author is 192.168.1.106, which is accessed by mobile phone,