Improve DEBUG/TRACE logging for Spring MVC

Issue: SPR-16898
This commit is contained in:
Rossen Stoyanchev
2018-06-18 17:48:55 -04:00
parent 003d643adc
commit 28a5c3009e
69 changed files with 585 additions and 602 deletions

View File

@@ -3272,26 +3272,8 @@ such headers.
[[mvc-links-to-controllers-from-views]]
=== Links in views
You can also build links to annotated controllers from views such as JSP, Thymeleaf,
FreeMarker. This can be done using the `fromMappingName` method in `MvcUriComponentsBuilder`
which refers to mappings by name.
Every `@RequestMapping` is assigned a default name based on the capital letters of the
class and the full method name. For example, the method `getFoo` in class `FooController`
is assigned the name "FC#getFoo". This strategy can be replaced or customized by creating
an instance of `HandlerMethodMappingNamingStrategy` and plugging it into your
`RequestMappingHandlerMapping`. The default strategy implementation also looks at the
name attribute on `@RequestMapping` and uses that if present. That means if the default
mapping name assigned conflicts with another (e.g. overloaded methods) you can assign
a name explicitly on the `@RequestMapping`.
[NOTE]
====
The assigned request mapping names are logged at TRACE level on startup.
====
The Spring JSP tag library provides a function called `mvcUrl` that can be used to
prepare links to controller methods based on this mechanism.
In views such as Thymeleaf, FreeMarker, JSP you can build links to annotated controllers
by referring to the implicitly or explicitly assigned name for each request mapping.
For example given:
@@ -3316,10 +3298,16 @@ You can prepare a link from a JSP as follows:
<a href="${s:mvcUrl('PAC#getAddress').arg(0,'US').buildAndExpand('123')}">Get Address</a>
----
The above example relies on the `mvcUrl` JSP function declared in the Spring tag library
(i.e. META-INF/spring.tld). For more advanced cases (e.g. a custom base URL as explained
in the previous section), it is easy to define your own function, or use a custom tag file,
in order to use a specific instance of `MvcUriComponentsBuilder` with a custom base URL.
The above example relies on the `mvcUrl` function declared in the Spring tag library
(i.e. META-INF/spring.tld), but it is easy to define your own function, or prepare a
similar one for other templating technologies.
Here is how this works. On startup every `@RequestMapping` is assigned a default name
through a `HandlerMethodMappingNamingStrategy` whose default implementation uses the
capital letters of the class and the method name, e.g. the `getFoo` method in
`FooController` becomes "FC#getFoo". If there is a name clash you can use
`@RequestMapping(name="..")` to assign an explicit name, or implement your own
`HandlerMethodMappingNamingStrategy`.