Uploaded image for project: '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
    • Status: Closed
    • Priority: 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";
          }
      
      }
      

        Attachments

          Activity

            People

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

              Dates

              • Created:
                Updated:
                Resolved: