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 Across Web 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 |
Webjar includes, see webjars support for more information. |
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 |
WebAppLinkBuilder
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. |