Spring Framework
  1. Spring Framework
  2. SPR-5391

Add ControllerMethodRequestToViewNameTranslator

    Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 3.0 M1
    • Fix Version/s: General Backlog
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      In most cases, the convention of mapping an internal @Controller method name to a controller-relative view name is superior to mapping based on the request path.
      For example, consider the requirement to have a HomeController mapped to the root application URL e.g. /petclinic. With method name-based view name translation, a handler method with name "home" will map to a view named "home", which is what we want. Combine this with controller-relative view resolution, and the home template can be packaged together in the same module as the @Controller. With today's default request-to-view-name-translator, the view name will be an empty string, which is not what we want.

      We should add a ControllerMethodToViewNameTranslator to Spring 3 or consider whether the DefaultRequestToViewNameTranslator should change to incorporate this functionality.
      This work should ideally be done at the same time as the support for controller-relative views.

      Basic ControllerMethodViewNameTranslator implementation shown below that assumes @Controller invocation context has been exposed in the request:

      public class ControllerMethodToViewNameTranslator implements
      RequestToViewNameTranslator {

      private DefaultRequestToViewNameTranslator defaultTranslator = new DefaultRequestToViewNameTranslator();

      public String getViewName(HttpServletRequest request) throws Exception {
      String methodName = (String) request.getAttribute("controllerMethod");
      if (methodName != null)

      { return methodName; }

      else

      { return defaultTranslator.getViewName(request); }

      }

      }

        Issue Links

          Activity

          Hide
          Keith Donald added a comment -

          Here's an side effect I noticed that illustrates another advantage of using the method name as the view name when we can resolve controller-relative views by convention:

          Consider this example:

          package rewardsonline.accounts;

          import org.springframework.stereotype.Controller;
          import org.springframework.web.bind.annotation.RequestMapping;

          @Controller
          public class AccountSearchController {

          @RequestMapping
          public void newSearch() {

          }
          }

          With this example /accountsearch/newSearch invokes the method and renders the accountsearch/newSearch view.
          Now consider a request sent to another URL in the /accountsearch namespace e.g. /accountsearch/foo.
          This request will be handled by the same method, yet the view that will be rendered will be accountsearch/foo.
          I find it odd that the view can change even though the same handler method handled the request.
          With a convention based on the method name, at least the newSearch view would be rendered again until I was more explicit about the URL mapping.

          Show
          Keith Donald added a comment - Here's an side effect I noticed that illustrates another advantage of using the method name as the view name when we can resolve controller-relative views by convention: Consider this example: package rewardsonline.accounts; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class AccountSearchController { @RequestMapping public void newSearch() { } } With this example /accountsearch/newSearch invokes the method and renders the accountsearch/newSearch view. Now consider a request sent to another URL in the /accountsearch namespace e.g. /accountsearch/foo. This request will be handled by the same method, yet the view that will be rendered will be accountsearch/foo. I find it odd that the view can change even though the same handler method handled the request. With a convention based on the method name, at least the newSearch view would be rendered again until I was more explicit about the URL mapping.
          Hide
          allnightlong added a comment -

          my vote for this issue

          Show
          allnightlong added a comment - my vote for this issue

            People

            • Assignee:
              Unassigned
              Reporter:
              Keith Donald
              Last updater:
              Trevor Marshall
            • Votes:
              5 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                2 years, 26 weeks, 4 days ago