본문 바로가기

Dev/Spring

Spring에서 Device별(desktop, mobile, tablet)로 접근 구분하는 방법(spring 3.2.x)

728x90
반응형

클라이언트 단에서 Device 사용자를 구분할수도 있지만 서버사이드 단에서 페이지를 나눠서 보여줘야 할 경우가 있는 경우 구분을 통해서 페이지별을 보여지게 할수 있다.

 

1. 먼저 Spring mobile 라이브러리를 받는다.(maven)   

<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>

4. Controller에 Divice 구분을 통해 설정

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

 

위의 설정으로 Device 구분을 통해 서비스를 제공할수 있다.

728x90
반응형