Skip to content

Commit 7885a59

Browse files
committed
Add section on building controller links from views
1 parent 51a550b commit 7885a59

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

src/asciidoc/index.adoc

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ method has been added.
10821082
with method `handleFoo` is named "FC#handleFoo". The naming strategy is pluggable.
10831083
It is also possible to name an `@RequestMapping` explicitly through its name attribute.
10841084
A new `mvcUrl` function in the Spring JSP tag library makes this easy to use in JSP pages.
1085+
See <<mvc-links-to-controllers-from-views>>.
10851086
* `ResponseEntity` provides a builder-style API to guide controller methods
10861087
towards the preparation of server-side responses, e.g. `ResponseEntity.ok()`.
10871088
* `RequestEntity` is a new type that provides a builder-style API to guide client-side REST
@@ -32574,7 +32575,7 @@ Therefore the use of flash attributes is recommended mainly for redirect scenari
3257432575

3257532576

3257632577

32577-
[[mvc-construct-encode-uri]]
32578+
[[mvc-uri-building]]
3257832579
=== Building URIs
3257932580

3258032581
Spring MVC provides a mechanism for building and encoding a URI using
@@ -32649,8 +32650,8 @@ also have the literal part of the servlet mapping included:
3264932650
.path("/accounts").build()
3265032651
----
3265132652

32652-
[[mvc-construct-uri-controllers]]
32653-
=== Building URIs to Controllers and methods
32653+
[[mvc-links-to-controllers]]
32654+
==== Building URIs to Controllers and methods
3265432655

3265532656
Spring MVC provides another mechanism for building and encoding URIs that link to
3265632657
Controllers and methods defined within an application.
@@ -32697,6 +32698,54 @@ URIs by coding against the actual Controller's API:
3269732698
URI uri = uriComponents.encode().toUri();
3269832699
----
3269932700

32701+
[[mvc-links-to-controllers-from-views]]
32702+
==== Building URIs to Controllers and methods from views
32703+
32704+
It is also useful to build links to annotated controllers from views (e.g. JSP).
32705+
This can be done through a method on `MvcUriComponentsBuilder` which refers to mappings
32706+
by name called `fromMappingName`.
32707+
32708+
As of 4.1 every `@RequestMapping` is assigned a default name based on the
32709+
capital letters of the class and the full method name. For example, the method `getFoo` in class
32710+
`FooController` is assigned the name "FC#getFoo". This naming strategy is pluggable
32711+
by implementing `HandlerMethodMappingNamingStrategy` and configuring it on your
32712+
`RequestMappingHandlerMapping`. Furthermore the `@RequestMapping` annotation includes
32713+
a name attribute that can be used to override the default strategy.
32714+
32715+
[NOTE]
32716+
====
32717+
The assigned request mapping names are logged at TRACE level on startup.
32718+
====
32719+
32720+
The Spring JSP tag library provides a function called `mvcUrl` that can be used to
32721+
prepare links to controller methods based on this mechanism.
32722+
32723+
For example given:
32724+
32725+
[source,java,indent=0]
32726+
[subs="verbatim,quotes"]
32727+
----
32728+
@RequestMapping("/people/{id}/addresses")
32729+
public class MyController {
32730+
32731+
@RequestMapping("/{country}")
32732+
public HttpEntity getAddress(@PathVariable String country) { ... }
32733+
}
32734+
----
32735+
32736+
The following JSP code can prepare a link:
32737+
32738+
[source,jsp,indent=0]
32739+
[subs="verbatim,quotes"]
32740+
----
32741+
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
32742+
...
32743+
<a href="${s:mvcUrl('PC#getPerson').arg(0,'123').build()}">Get Person</a>
32744+
----
32745+
32746+
32747+
32748+
3270032749

3270132750
[[mvc-localeresolver]]
3270232751
=== Using locales

0 commit comments

Comments
 (0)