Spring: detects the types of devices that access the website.

Source: Internet
Author: User

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
  1. ── Src
  2. ── Main
  3. ── Java
  4. ── 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
    1. 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,


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.