Spring Framework
  1. Spring Framework
  2. SPR-7812

Allow custom request mapping conditions

    Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Complete
    • Affects Version/s: 3.0.5
    • Fix Version/s: 3.1 RC1
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      As outlined by a user here: https://jira.springframework.org/browse/MOBILE-8, it would be useful if it was possible to map requests to @Controller methods by criteria other than path, parameter value, or header value. MOBILE-8 shrewdly suggests support for mapping by SitePreference, to allow different @Controller methods to handle requests for different versions of the application's "site", such as the "mobile" site vs. the "normal" site.

      Rather than baking in additional hard-coded mapping options, I recommend we consider adding support for mapping by boolean SpEL Expression, which can be used in conjunction with the other @RequestMapping attributes. For example:

      @RequestMapping(value="/", method=RequestMethod.GET, expression="currentSitePreference.mobile")
      public String homeMobile(Model model) {
          // prepare model needed by the mobile view
          return "home-mobile";
      }
      
      @RequestMapping(value="/", method=RequestMethod.GET)
      public String home(Model model) {
          // prepare normal model
          return "home";
      }
      

        Issue Links

          Activity

          Hide
          Rossen Stoyanchev added a comment - - edited

          Modified title from "Allow @RequestMapping by boolean SpEL expression" to "Allow custom request mapping conditions" to describe better the actual way the resolution has evolved. Below is a high-level description of how it works.

          Show
          Rossen Stoyanchev added a comment - - edited Modified title from "Allow @RequestMapping by boolean SpEL expression" to "Allow custom request mapping conditions" to describe better the actual way the resolution has evolved. Below is a high-level description of how it works.
          Hide
          Rossen Stoyanchev added a comment -

          Note that in Spring Mobile the StandardSitePreferenceHandler (invoked from a HandlerInterceptor) saves the current SitePreference in a request attribute. This attribute is not available at request mapping time, which takes place before a handler is selected and hence before HandlerInterceptors are invoked.

          Instead a custom SitePreferenceRequestCondition would need to invoke the SitePreferenceRepository directly or check the "site_preference" request parameter.

          Show
          Rossen Stoyanchev added a comment - Note that in Spring Mobile the StandardSitePreferenceHandler (invoked from a HandlerInterceptor) saves the current SitePreference in a request attribute. This attribute is not available at request mapping time, which takes place before a handler is selected and hence before HandlerInterceptors are invoked. Instead a custom SitePreferenceRequestCondition would need to invoke the SitePreferenceRepository directly or check the "site_preference" request parameter.
          Hide
          Keith Donald added a comment -

          Ouch, that's unfortunate. More and more it seems to me we need a general filter capability in Spring MVC...

          Show
          Keith Donald added a comment - Ouch, that's unfortunate. More and more it seems to me we need a general filter capability in Spring MVC...
          Hide
          Rossen Stoyanchev added a comment -

          Type and method-level, custom request conditions can now be provided by overriding protected methods in RequestMappingHandlerMapping.

          Show
          Rossen Stoyanchev added a comment - Type and method-level, custom request conditions can now be provided by overriding protected methods in RequestMappingHandlerMapping.
          Hide
          Rossen Stoyanchev added a comment -

          Marking this as resolved since the ability to add a custom request mapping condition is now available. I've also added a comment under SPR-8468 as it relates to the need for interception independent of the mapped handler.

          Show
          Rossen Stoyanchev added a comment - Marking this as resolved since the ability to add a custom request mapping condition is now available. I've also added a comment under SPR-8468 as it relates to the need for interception independent of the mapped handler.
          Hide
          Marek Sebera added a comment -

          Rossen: Would it be please possible to link this resolution to example of use and/or appropriate documentation?

          Show
          Marek Sebera added a comment - Rossen: Would it be please possible to link this resolution to example of use and/or appropriate documentation?

            People

            • Assignee:
              Rossen Stoyanchev
              Reporter:
              Keith Donald
              Last updater:
              Trevor Marshall
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                2 years, 1 day ago