Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-12617

Allow customizing "base" path in MvcUriComponentsBuilder (e.g. path prefix for locale)

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 4.1.4
    • Fix Version/s: 4.2 RC1
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Google recommends to use country specific (toplevel) domains, subdomains or subdirectories to indicate the locale of a sites content.

      Spring provides out of the box support for url parameters to change the locale of a site (explicitly not recommended by google) when using a LocaleChangeInterceptor. It should be relatively easy to create an interceptor that extracts the locale from the TLD or a subdomain, but creating "directories" that indicate the locale is not possible as of spring 4.1.4 in a noninvasive way.

      The urls should look like this:
      http://www.example.com/en/foo

      It is easy to write an interceptor/localeResolver to extract the locale part from the url. But request mapping becomes a pain, because every controller must be aware of the locale that is present in the url. It becomes more difficult, if one would like
      http://www.example.com/foo
      to point to an automatically evaluated locale (AcceptHeaderLocale, IP lookup, default locale).

      I tried to solve the problem by extending UrlPathHelper to strip the locale part from the url. Then the mapping becomes easy again.

      The problem is, that the MvcUriComponentsBuilder is not aware of the fact that there is some stripped part of the url. At least when using the spring url tag library to construct urls from controller methods (the tag utilizes MvcUriComponentsBuilder#fromMappingName), the stripped part is lost. I didn't test it, but all factory methods of MvcUriComponentsBuilder should lose the locale part.

      It should be possible to to configure a strategy to manipulate the retrieved url path, like it is possible for controller method arguments using UriComponentsContributor s. With such a strategy, it should be possible to restore the stripped part of the url.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              cmrudolph Christian Rudolph
              Last updater:
              Spring Issuemaster
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                1 year, 40 weeks, 6 days ago