Spring: Probing the type of device that accesses a Web site

Source: Internet
Author: User

[The tutorial is translated from the official spring and is properly truncated.] ]

You will build the

Create a spring MVC project to probe the device types (phones, computers, tablets) that access the site, and dynamically toggle the rendered view.

Tools

A text editor, JDK1.6 and above, Maven 3.0+ or Gradle 1.11+. (This article will use MAVEN)

pom.xml list:

<?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 "> <modelVersion>4.0.0</modelVersion> <groupId> Org.springframework</groupid> <artifactId>gs-device-detection</artifactId> <version> 0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifact     Id>spring-boot-starter-parent</artifactid> <version>1.1.5.RELEASE</version> </parent>            <dependencies> <dependency> <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency>     <groupId>org.springframework.mobile</groupId>       <artifactId>spring-mobile-device</artifactId> </dependency> </dependencies> &L T;properties> <start-class>hello.                 application</start-class> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> </plugin> <plugi N> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot- maven-plugin</artifactid> </plugin> </plugins> </build> &LT;REPOSITORIES&G        T <repository> <id>spring-releases</id> <name>spring Milestone repository</ name> <url>http://repo.spring.io/libs-release</url> </repository> </repositor ies> <pluginRepositories> <pluginRepository> &Lt;id>spring-releases</id> <name>spring Milestone repository</name> <url>h Ttp://repo.spring.io/libs-release</url> </pluginRepository> </pluginrepositories></project& Gt

New Project
First you create a directory structure that conforms to the MAVEN specification, Src/main/java/hello

[Plain]View Plaincopy
  1. └──src
  2. └──main
  3. └──java
  4. └──hello
Automatic configuration:

Observing Pom.xml, we found a new spring mobile dependency, so spring Boot will automatically configure Deviceresolverhandlerinterceptor and Devicehandlermethodargumentresolver. The former (Deviceresolverhandlerinterceptor) in the request to check the User-agent head, through the head information, can be judged from the phone or computer or tablet. The latter (devicehandlermethodargumentresolver) allows spring MVC to use the parsed device object within 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, instead of relying on a view technique (such as a JSP) in HTML to parse the data, it writes the string directly to the HTTP response. @ResponseBody annotations tell spring MVC to write a return object to the response body instead of rendering a model to be 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);}    }

This is also where the app is run in spring's built-in Tomcat. @ComponentScan is to tell spring to recursively search the class with the @component annotation under the Hello packet, so that Devicedetectioncontroller is found and registered, Because @controller is also a kind of @component.

@EnableAutoConfiguration is based on the contents of the current class load path, switching to a reasonable default configuration. This application requires the default built-in Tomcat configuration (Tomcat-embed-core.jar) and spring MVC default configuration (Spring-webmvc.jar).


The method of packaging execution is similar to the previous article, [Plain]View Plaincopy
    1. MVN Clean Package
And then

Java-jar Target/gs-device-detection-0.1.0.jar

In the event of an accident, the console appears:



Computer Browser access:


The author IP is 192.168.1.106, using mobile Access,


Spring: Probing the type of device that accesses a Web site

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.