Spring Mobile Device is a library that provides a simple way to detect the device type of the user. It is based on the user-agent string of the request. It is a part of the Spring Mobile project.
Core Components of Spring Mobile
Automatic Device Detection
Spring Mobile Device provides automatic device detection. It detects the device type of the user based on the user-agent string of the request. It provides a DeviceResolver interface that can be used to customize the device detection.
code example
public class DeviceResolverHandlerInterceptor implements HandlerInterceptor {
private DeviceResolver deviceResolver;
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Device device = deviceResolver.resolveDevice(request);
request.setAttribute(DEVICE_ATTRIBUTE, device);
return true;
}
}
Site Preference Management:
Spring Mobile Device provides a SitePreferenceResolver interface that can be used to customize the site preference management.
SITE_PREFERENCE_ATTRIBUTE
public class SitePreferenceHandlerInterceptor implements HandlerInterceptor {
private SitePreferenceResolver sitePreferenceResolver;
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
SitePreference sitePreference = sitePreferenceResolver.resolveSitePreference(request);
request.setAttribute(SITE_PREFERENCE_ATTRIBUTE, sitePreference);
return true;
}
}
Site Switching
Spring Mobile Device provides a SiteSwitcher interface that can be used to switch sites.
code example
public class SiteSwitcherHandlerInterceptor implements HandlerInterceptor {
private SiteSwitcher siteSwitcher;
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
SitePreference sitePreference = siteSwitcher.switchSite(request);
request.setAttribute(SITE_PREFERENCE_ATTRIBUTE, sitePreference);
return true;
}
}
Device Aware View Manager
Spring Mobile Device provides a DeviceAwareViewManager interface that can be used to detect the user device type.
code example
public class DeviceAwareViewManagerImpl implements DeviceAwareViewManager {
private Map<Device, String> mobilePrefixes = new HashMap<Device, String>();
private String defaultPrefix;
public String getViewName(String viewName, Device device) {
String prefix = mobilePrefixes.get(device);
if (prefix == null) {
prefix = defaultPrefix;
}
return prefix + viewName;
}
}
How it Works
We need maven dependency from the repository
Maven Dependency
<dependency>
<groupId>org.springframework.mobile</groupId>
<artifactId>spring-mobile-device</artifactId>
<version>1.1.5.RELEASE</version>
</dependency>
Repository
From personal testing, I found out the device instance can be null. So to make things easier I would prefer the current dependency used not to change because it works well with the current version of spring boot except for stable releases.Check what it means to be release or snapshot maven-snapshot-release-repository
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
Configuration
@Configuration
@EnableWebMvc
@EnableSpringConfigured
@ComponentScan(basePackages = "com.fas")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
return new DeviceResolverHandlerInterceptor();
}
@Bean
public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {
return new DeviceHandlerMethodArgumentResolver();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(deviceResolverHandlerInterceptor());
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(deviceHandlerMethodArgumentResolver());
}
}
Device Resolver Fallback resolution
This is the default implementation of the DeviceResolver interface. It uses the user-agent string of the request to detect the device type of the user. It also provides a fallback resolution mechanism that can be used to customize the device detection.
public interface DeviceResolver {
Device resolveDevice(HttpServletRequest request);
}
The device interface returns a device types.
public interface Device {
boolean isNormal();
boolean isMobile();
boolean isTablet();
}
Controller
@Controller
public class DeviceController {
@RequestMapping("/device")
public String device(@RequestParam(value = "name", required = false, defaultValue = "Fas") String name, Model model, Device device) {
model.addAttribute("name", name);
model.addAttribute("device", device);
return "device";
}
}
View
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Device</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1 th:text="'User, ' + ${name} + '!'" />
<h2 th:text="'Device: ' + ${device}" />
</body>
</html>
Result
User, Fas!
Device: MOBILE | TABLET | NORMAL
Conclusion
Spring Mobile Device provides automatic device detection, site preference management, site switching, and device aware view management. It also provides a DeviceResolver interface that can be used to customize the device detection. It also provides a SitePreferenceResolver interface that can be used to customize the site preference management. It also provides a SiteSwitcher interface that can be used to customize the site switching. It also provides a DeviceAwareViewManager interface that can be used to customize the device aware view management.
Notice that there is alternatives to the device resolution or getting properties of device. refer to the link below for more information. Alternative to Device Resolution
Reference
'Spring Boot' 카테고리의 다른 글
TESTCONTIANERS (0) | 2023.05.04 |
---|---|
SER.1. How to Create Multi-Project Maven POMs (3) | 2023.04.05 |
How to trigger Mono execution after another Mono terminates (0) | 2023.03.09 |