메뉴 건너뛰기

XEDITION

spring

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. action-servlet.xml에서 아래 내용을 추가한다.

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

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!";
    }
위로