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 :
728x90
반응형
'Study > Spring' 카테고리의 다른 글
[MySql] MySQL DateTime 포맷과 문자열 날짜값의 비교, 그리고 date_format() 날짜 포매팅 (0) | 2020.03.23 |
---|---|
@Transactional 정리 (0) | 2020.03.23 |
web.xml / root-context.xml / servlet-context.xml 란? (0) | 2020.02.14 |
Spring을 이용해 간단한 스케쥴러 만들기 (0) | 2020.02.06 |
1. Spring Batch 가이드 - 배치 어플리케이션이란? (0) | 2020.01.30 |
댓글