Spring Mobile
  1. Spring Mobile
  2. MOBILE-35

Provide a device aware view resolver to manage view selection based on device type

    Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Major Major
    • Resolution: Complete
    • Affects Version/s: None
    • Fix Version/s: 1.1.0.M2
    • Component/s: device
    • Security Level: Public
    • Labels:
      None

      Description

      As far as I can tell, the current site preference handling assumes the developer wants to redirect a mobile or tablet user to a different URL. In some cases it may be useful to keep the URL the same and simply render a different view depending on the device type. There are examples of handling this logic inside of a @Controller method. However, there are situations where delegating this responsibility to the controller may not be desirable. For instance, an application using Spring Web Flow tends to require view names to be statically declared in a flow.xml file.

      In simpler cases, using a coding by convention approach, can remove the responsibility from the controller of picking the view name based on device type leaving this up to a convention specified by a handler interceptor. For example, consider the following code from Spring Mobile's documentation:

      @Controller
      public class HomeController {
      
          @RequestMapping("/")
          public String home(SitePreference sitePreference, Model model) {
              if (sitePreference == SitePreference.MOBILE) {
                  // prepare mobile view for rendering
                  return "home-mobile";
              } else {
                  // prepare normal view for rendering
                  return "home";
              }
          }
      
      }
      

      Here, the controller is burdened with picking the view name. However, the controller logic may be the same for both the normal site and the mobile site. Therefore, providing developers with an option of managing the view selection using a handler interceptor cuts down on boilerplate code in the controller.

      If the view was adjusted by a handler interceptor such as:

      public final class MobileViewHandlerInterceptor extends HandlerInterceptorAdapter implements HandlerInterceptor {
      	
      	public void postHandle(
      			HttpServletRequest request,
      			HttpServletResponse response, Object handler,
      			ModelAndView modelAndView) throws Exception {
      	
      		final Device device = // get the device
      		
      		if(modelAndView != null && modelAndView.getViewName() != null) {
      			
      			if(device.isMobile()) {
      				modelAndView.setViewName("mobile/" + modelAndView.getViewName());
      			}
      		}
      	}
      }
      

      The controller could be simplified to:

      @Controller
      public class HomeController {
      
          @RequestMapping("/")
          public String home(Model model) {
              // prepare view for rendering
              return "home";
          }
      
      }
      

        Activity

        Hide
        Roy Clarkson added a comment -

        Scott, I have spent some time on it, but it's not far along. I'm in the process of wrapping up a 1.0.1 release, which will hopefully go out soon. I decided this enhancement and a few other items should go in a milestone release instead, so we can get feedback and modify if necessary before the final release of the functionality. I'm happy to collaborate on it in a separate branch.

        Show
        Roy Clarkson added a comment - Scott, I have spent some time on it, but it's not far along. I'm in the process of wrapping up a 1.0.1 release, which will hopefully go out soon. I decided this enhancement and a few other items should go in a milestone release instead, so we can get feedback and modify if necessary before the final release of the functionality. I'm happy to collaborate on it in a separate branch.
        Hide
        Roy Clarkson added a comment -

        I've submitted a pull request that implements this functionality. It's a ViewResolver wrapper that delegates to a single internal ViewResolver, defined in the configuration. It supports the LiteDevice, meaning it handles normal, mobile, and tablet views. It also supports both prefix and suffix strategies, or both if desired.

        Rossen Stoyanchev and Scott Rossillo in particular, I would appreciate your feedback. Thanks.

        Show
        Roy Clarkson added a comment - I've submitted a pull request that implements this functionality. It's a ViewResolver wrapper that delegates to a single internal ViewResolver, defined in the configuration. It supports the LiteDevice, meaning it handles normal, mobile, and tablet views. It also supports both prefix and suffix strategies, or both if desired. Rossen Stoyanchev and Scott Rossillo in particular, I would appreciate your feedback. Thanks.
        Hide
        Neale Upstone added a comment -

        Hi Roy,

        Thanks for that. I've just had a quick scan over the pull request, and it satisfies the pattern I'm using, which is to have desktop/ and mobile/ view prefixes.

        I'm being pretty quirky with mine in that for a mobile device, I fall back to using the desktop view if the mobile view doesn't exist (which I can do as I'm using Tiles). I'm pretty sure I'll be able to use your approach when this pull request is merged into a milestone release

        Show
        Neale Upstone added a comment - Hi Roy, Thanks for that. I've just had a quick scan over the pull request, and it satisfies the pattern I'm using, which is to have desktop/ and mobile/ view prefixes. I'm being pretty quirky with mine in that for a mobile device, I fall back to using the desktop view if the mobile view doesn't exist (which I can do as I'm using Tiles). I'm pretty sure I'll be able to use your approach when this pull request is merged into a milestone release
        Hide
        Roy Clarkson added a comment -

        Thanks for the feedback Neale. FYI, Rossen I and are discussing some minor modifications over on the github page. Actually, Tiles is a concern too. I need to set up a sample app to vet that configuration too.

        Show
        Roy Clarkson added a comment - Thanks for the feedback Neale. FYI, Rossen I and are discussing some minor modifications over on the github page. Actually, Tiles is a concern too. I need to set up a sample app to vet that configuration too.
        Hide
        Roy Clarkson added a comment -

        Added LiteDeviceDelegatingViewResolver for managing view selection based on device type.

        Show
        Roy Clarkson added a comment - Added LiteDeviceDelegatingViewResolver for managing view selection based on device type.

          People

          • Assignee:
            Roy Clarkson
            Reporter:
            Scott Rossillo
          • Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: