본문 바로가기
Study/Spring

Device별 (desktop, mobile, tablet)로 접근 구분방법

by 오늘만 사는 여자 2020. 2. 14.
728x90
반응형

Spring에서 간단하게 접속하는 Device(desktop, mobile, tablet)를 구분하는 방법이 있다.

 

1. 먼저 Spring mobile 라이브러리를 받는다. pom.xml에 추가

<dependency>
 <groupId>org.springframework.mobile</groupId>
 <artifactId>spring-mobile-device</artifactId>
 <version>1.1.0.RELEASE</version>
</dependency>

2. servlet-context.xml에 아래 내용을 추가한다. 

 

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

그래서 상단에도 하기처럼 되어있어야 한다. 

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 

3. web.xml에서 필터링 부분을 추가한다.

 

<filter>
  <filter-name>deviceResolverRequestFilter</filter-name>
  <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>deviceResolverRequestFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

4. Controller에서 Device구분

 

import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.DeviceUtils;
@RequestMapping("/hello")
    public @ResponseBody String detectDevice(HttpServletRequest request) {        
        Device device = DeviceUtils.getCurrentDevice(request);        
        if (device == null) {
            return "device is null";
        }
        String deviceType = "unknown";
        if (device.isNormal()) {
            deviceType = "nomal";
        } else if (device.isMobile()) {
            deviceType = "mobile";
        } else if (device.isTablet()) {
            deviceType = "tablet";
        }
        return "Hello " + deviceType + " browser!";
}

 

Reference : 

https://yookeun.github.io/java/2014/09/26/spring-device/

728x90
반응형

댓글