当前位置:   article > 正文

Spring moble 自定义Device接口_org.springframework.mobile.device.device

org.springframework.mobile.device.device

在Spring moble框架中提供了一个Device的接口

  1. package org.springframework.mobile.device;
  2. /**
  3. * A model for the user agent or device that submitted the current request.
  4. * Callers may introspect this model to vary UI control or rendering logic by device type.
  5. * @author Keith Donald
  6. * @author Roy Clarkson
  7. * @author Scott Rossillo
  8. * @author Onur Kagan Ozcan
  9. */
  10. public interface Device {
  11. /**
  12. * True if this device is not a mobile or tablet device.
  13. */
  14. boolean isNormal();
  15. /**
  16. * True if this device is a mobile device such as an Apple iPhone or an Nexus One Android.
  17. * Could be used by a pre-handle interceptor to redirect the user to a dedicated mobile web site.
  18. * Could be used to apply a different page layout or stylesheet when the device is a mobile device.
  19. */
  20. boolean isMobile();
  21. /**
  22. * True if this device is a tablet device such as an Apple iPad or a Motorola Xoom.
  23. * Could be used by a pre-handle interceptor to redirect the user to a dedicated tablet web site.
  24. * Could be used to apply a different page layout or stylesheet when the device is a tablet device.
  25. */
  26. boolean isTablet();
  27. /**
  28. *
  29. * @return resolved DevicePlatform
  30. */
  31. DevicePlatform getDevicePlatform();
  32. }

想要拓展这个接口,加上自己的deivce

  1. package eu.digient.billfold.support.web
  2. import org.springframework.mobile.device.Device
  3. import org.springframework.mobile.device.DevicePlatform
  4. import eu.digient.sdk.util.DeviceType
  5. interface TitanDevice extends Device {
  6. boolean isMobileApp()
  7. }
  8. class TitanDeviceImpl implements TitanDevice {
  9. private final Device device
  10. private final DeviceType deviceType
  11. TitanDeviceImpl(Device device) {
  12. this.device = device
  13. }
  14. TitanDeviceImpl(Device device, DeviceType deviceType) {
  15. this.device = device
  16. this.deviceType = deviceType
  17. }
  18. boolean isNormal() {
  19. return deviceType == DeviceType.Desktop ?: device?.isNormal()
  20. }
  21. boolean isMobile() {
  22. return deviceType == DeviceType.Mobile ?: device?.isMobile()
  23. }
  24. boolean isTablet() {
  25. return deviceType == DeviceType.Tablet ?: device?.isTablet()
  26. }
  27. boolean isMobileApp() {
  28. return deviceType == DeviceType.MobileApp
  29. }
  30. DevicePlatform getDevicePlatform() {
  31. return device?.getDevicePlatform()
  32. }
  33. }

对外提供两个构造方法用来辨别deviceType, 在重写一下LiteDeviceResolver里面的resolveDevice方法这里面可以加上自己的逻辑。
 

  1. class InternalDeviceResolver extends LiteDeviceResolver {
  2. private static final String USER_AGENT_HEADER_NAME = 'userAgent'
  3. private static final String USER_AGENT_MOBILE_APP = 'XXXXX'
  4. @Override
  5. Device resolveDevice(HttpServletRequest request) {
  6. def device = super.resolveDevice(request)
  7. def ua = request.getHeader(USER_AGENT_HEADER_NAME)
  8. if (ua != null && ua.toLowerCase().indexOf(USER_AGENT_MOBILE_APP) >= 0) {
  9. return new TitanDeviceImpl(device, DeviceType.MobileApp)
  10. }
  11. return new TitanDeviceImpl(device)
  12. }
  13. }

接着在Springmvc.xml 配置文件中添加 InternalDeviceResolver 拦截器:

  1. <mvc:interceptors>
  2. <!-- On pre-handle, resolve the device that originated the web request -->
  3. <bean class="eu.digient.billfold.support.web.InternalDeviceResolverHandlerInterceptor"/>
  4. </mvc:interceptors>

使用自定义的define拦截器而不是spring拦截器从请求解析设备

  1. class InternalDeviceResolverHandlerInterceptor extends HandlerInterceptorAdapter {
  2. private final InternalDeviceResolver deviceResolver
  3. InternalDeviceResolverHandlerInterceptor(InternalDeviceResolver deviceResolver) {
  4. this.deviceResolver = deviceResolver
  5. }
  6. InternalDeviceResolverHandlerInterceptor() {
  7. this(new InternalDeviceResolver())
  8. }
  9. @Override
  10. boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  11. Device device = deviceResolver.resolveDevice(request)
  12. request.setAttribute(DeviceUtils.CURRENT_DEVICE_ATTRIBUTE, device)
  13. return true
  14. }
  15. }

这样我们在解析device的时候,只需要把device 强转成 TitanDevice就好了

  1. class DeviceResolverImpl implements DeviceResolver {
  2. DeviceType resolveDevice(final Device device) {
  3. TitanDevice titanDevice = device as TitanDevice
  4. if (titanDevice?.isMobileApp()) {
  5. return DeviceType.MobileApp
  6. } else if (titanDevice?.isTablet()) {
  7. return DeviceType.Tablet
  8. } else if (titanDevice?.isMobile()) {
  9. return DeviceType.Mobile
  10. }
  11. return DeviceType.Desktop
  12. }
  13. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/335175
推荐阅读
相关标签
  

闽ICP备14008679号