Spring Mobile detects multiple device types

Original Link: https://blog.csdn.net/hellowordapi/article/details/78395295

The spring mobile framework is a small framework for getting device information and jumping to different views.

Integration method

1. What is pring Moblie?

SpringMoblie is an extension of SpringMvc designed to simplify the development of mobile web applications.Open source address is https://github.com/spring-projects/spring-mobile

2. What can Spring Moblie do?

It detects which device initiated the request to access the current server program (PC, tablet, mobile phone) and jumps to the view corresponding to the device, depending on which device was accessed.

Maven address:

<org.springframework.mobile-version>1.1.3.RELEASE</org.springframework.mobile-version>
   <!-- spring mobile -->
        <dependency>
            <groupId>org.springframework.mobile</groupId>
            <artifactId>spring-mobile-device</artifactId>
            <version>${org.springframework.mobile-version}</version>
        </dependency>

First, let's look at one of its most common interfaces:

package org.springframework.mobile.device;
 
/**
 This interface is used to detect the type of device currently requested
*/
public interface Device {
  
   /**
    This method is used to determine if the current client request is a PC-side
   */
   boolean isNormal();
 
   /**
    This method is used to determine if the current client request is a mobile device such as Apple Android
   */
  boolean isMobile();
    
   /**
    This method is used to determine if the current client request is a tablet device such as an iPad 
    */
  boolean isTablet();
}

Then add the DeviceResolverHandlerInterceptor interceptor to the Sprgmvc.xml configuration file:

   <mvc:interceptors>
        <beans:bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
    </mvc:interceptors>

Or you can configure Filter in web.xml:

<filter>
    <filter-name>deviceResolverRequestFilter</filter-name>
    <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>

Either of the above two configurations plays the same role. The first one I tested is configuring interceptors via mvc.

After configuring the above interceptors, you can get the current requested device type as follows:

	Device currentDevice = DeviceUtils.getCurrentDevice(servletRequest)

 

Of course, I'm sure you would prefer it to be passed automatically as a parameter in @RequestMapping("/") of @Controller(@RestController), as follows:

  @RequestMapping(value = "signin", method = RequestMethod.GET)
  @ResponseBody
  public  String signin(Device device){
   if(device.isNormal()){
          return "Access device type is PC";
      }
 
      if(device.isMobile()){
          return "Access device type is mobile device";
      }
      if (device.isTablet()){
          return "Access device type is tablet";
      }
      return null;
  }

Then you need to configure DeviceWebArgumentResolver as follows:

    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <beans:bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

2. User preferences and site management.

Spring Moblie allows a user to switch views across multiple sites in the application. For one reason, the current user uses a mobile UI for access, but the content mobile UI he wants to access does not provide views (or functions) and a view switch is required.

2.1 User clicks determine whether users prefer to visit normal or mobile sites:

<a href="${currentUrl}?Site_preference=normal">normal site</a> | <a href="${currentUrl}?Site_preference=mobile">mobile site</a>
After 2.2 clicks, Spring Moblie saves user preferences into cookies, through the Cookie SitePreferenceRepository class, and of course you can also save into cookies yourself or process them separately

2.3 Configure Interceptor SitePreferenceHandlerInterceptor:

<mvc:interceptors>
    <beans:bean class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />
</mvc:interceptors>

By default, SitePreferenceHandlerInterceptor saves user options into Cookies by default through CookieSitePreferenceRepository
The following is the SitePreferenceHandlerInterceptor code, where the default constructor creates a CookieSitePreferenceRepository and the constructor in CookieSitePreferenceRepository

setCookieName saves user options into the Cookie, of course you can insert another Site preference handler.

     

    

2.4 With the above configuration, you can use the following code or user preferences:

SitePreference sitePreference = SitePreferenceUtils.getCurrentSitePreference(servletRequest);

2.5 If you want to automatically pass as a parameter in @RequestMapping("/") of @Controller(@RestController), then you need to configure it as above:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <beans:bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />        
    </mvc:argument-resolvers>
</mvc:annotation-driven>   

 

Then use the following code:

     

@RequestMapping(value = "signin", method = RequestMethod.GET)
 
  public  String signin(SitePreference site){
       if(site==SitePreference.MOBILE){
           return "Preparing mobile views for users to render";
       }
 
       if(site==SitePreference.NORMAL){
           return "Prepare for users PC View Rendering";
       }
 
       if(site==SitePreference.TABLET){
           return"Prepare flat view for user rendering";
       }
      return null;
  }


2.6 Site Switching

If you want to place mobile users and PC users on two different sites for example, PC user access to example.com and mobile user access to example.app.com, you can configure the following for site switching:

<mvc:interceptors>
    <beans:bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
    <beans:bean class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor" factory-method="mDot">
        <beans:constructor-arg value="example.app.com" />
    </beans:bean>
</mvc:interceptors>

Spring Mobile will automatically help you switch sites, and of course you can set the factory-method to "urlPath", then it will redirect mobile users to url addresses, like the following:

<mvc:interceptors>
    <beans:bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
    <beans:bean class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor" factory-method="urlPath">
        <beans:constructor-arg value="/m" />
    </beans:bean>
</mvc:interceptors>


Spring Mobile will help you redirect to
   example.app.com/m
You can also specify that the root directory is added with a constructor under urlPath.It is important to note that they all inject the set values into Cookie preferences by default, and that they are shared between the mobile site and the normal site.

 

 

 

 

 

 

 

 

Tags: Mobile Spring xml github

Posted on Sun, 08 Sep 2019 19:37:43 -0700 by jordy