Generating URLs

WebAppPathResolver and path prefixing

Across Web introduces the PrefixingRequestMappingHandlerMapping. This allows @RequestMapping methods to be prefixed at runtime, based on configuration.

Some standard Across modules like AdminWebModule and DebugWebModule make use of this functionality.

Because the actual relative path of a controller is then not known during development, you can define a relative path with a prefix. If you provide your path to the WebAppPathResolver bean, the prefix will then be replaced at runtime by the final path.

The AcrossWebModule Thymeleaf dialect integrates with the WebAppPathResolver so prefix based paths are supported directly in templates.

If you want to ensure your relative path is never prefixed, start it with an exclamation mark (!).

Default prefixes

Across Web registers default prefixers for static resources:

Name Target

@resource

Static resources in the root resources folder.

@static

Static resources in the static subfolder. This is a short-hand for @resource:/static/<path>

@webjars

Takes a relative path to a webjar. By default mapped to /webjars but can be changed by setting the webjar path.

For example <link rel="stylesheet" type="text/css" th:href="@{@webjars:/bootstrap/3.1.0/ss/bootstrap.min.css}"/> becomes: <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/3.1.0/css/bootstrap.min.css">

Path resolving examples

The examples assume that res is the configured acrossWebModule.resources.path:

Input Output

/my/path

/my/path

@resource:/my/path

/res/my/path

@static:/my/path?id=10#home

/res/static/my/path?id=10#home

redirect:@resource/my/path

redirect:/res/my.path

!@resource/my/path

@resource/my/path

Registering your own prefixing handler mapping and resolver

See PrefixingHandlerMappingConfiguration and PrefixingPathContext.

The WebAppPathResolver will only replace the relative path prefix, but to generate a valid application link you would still need to prepend the possible context path as wel as encode the URL. If you want to generate a complete link from code, you can use the WebAppLinkBuilder bean.

You should only use the the WebAppLinkBuilder if your links will not be processed additionally. To generate redirect paths, or links for a spring:url or Thymeleaf link builder @{}, use the WebAppPathResolver.

Otherwise you could end up with links having the context path added twice.