SPR-7406 Improve documentation on handler method parameter type conversion.

This commit is contained in:
Rossen Stoyanchev 2011-03-04 19:59:16 +00:00
parent 386d8fdaa6
commit 56d50ba8a5
1 changed files with 52 additions and 15 deletions

View File

@ -603,23 +603,22 @@
public class HelloWorldController { public class HelloWorldController {
@RequestMapping("/helloWorld") @RequestMapping("/helloWorld")
public ModelAndView helloWorld() { public String helloWorld(Model model) {
ModelAndView mav = new ModelAndView(); model.addAttribute("message", "Hello World!");
mav.setViewName("helloWorld"); return "helloWorld";
mav.addObject("message", "Hello World!");
return mav;
} }
}</programlisting> }</programlisting>
<para>As you can see, the <interfacename>@Controller</interfacename> and <para>As you can see, the <interfacename>@Controller</interfacename> and
<interfacename>@RequestMapping</interfacename> annotations allow flexible <interfacename>@RequestMapping</interfacename> annotations allow flexible
method names and signatures. In this particular example the method has no method names and signatures. In this particular example the method accepts
parameters and returns a <classname>ModelAndView</classname>, but various a <classname>Model</classname> and returns a view name as a
other (and better) strategies exist, <!--strategies for doing *what*? -->as <classname>String</classname>, but various other method parameters and
are explained later in this section. <classname>ModelAndView</classname>, return values can be used as explained later in this section.
<interfacename>@Controller</interfacename>, and <interfacename>@Controller</interfacename> and
<interfacename>@RequestMapping</interfacename> form the basis for the <interfacename>@RequestMapping</interfacename> and a number of other
Spring MVC implementation. This section documents these annotations and annotations form the basis for the Spring MVC implementation.
This section documents these annotations and
how they are most commonly used in a Servlet environment.</para> how they are most commonly used in a Servlet environment.</para>
<section id="mvc-ann-controller"> <section id="mvc-ann-controller">
@ -679,7 +678,7 @@ public class HelloWorldController {
class-level annotation maps a specific request path (or path pattern) class-level annotation maps a specific request path (or path pattern)
onto a form controller, with additional method-level annotations onto a form controller, with additional method-level annotations
narrowing the primary mapping for a specific HTTP method request method narrowing the primary mapping for a specific HTTP method request method
("GET"/"POST") or specific HTTP request parameters.</para> ("GET", "POST", etc.) or an HTTP request parameter condition.</para>
<para>The following example shows a controller in a Spring MVC <para>The following example shows a controller in a Spring MVC
application that uses this annotation:</para> application that uses this annotation:</para>
@ -894,8 +893,8 @@ public class RelativePathUriTemplateController {
Date, etc. Spring automatically converts to the appropriate type and Date, etc. Spring automatically converts to the appropriate type and
throws a <classname>TypeMismatchException</classname> if the type is throws a <classname>TypeMismatchException</classname> if the type is
not correct. You can further customize this conversion process by not correct. You can further customize this conversion process by
customizing the data binder. See <xref customizing the data binder. See <xref linkend="mvc-ann-typeconversion"/>
linkend="mvc-ann-webdatabinder" />.</para> and <xref linkend="mvc-ann-webdatabinder" />.</para>
</tip> </tip>
</section> </section>
@ -1252,6 +1251,10 @@ public class EditPetForm {
<literal>required</literal> attribute to <literal>false</literal> <literal>required</literal> attribute to <literal>false</literal>
(e.g., <literal>@RequestParam(value="id", (e.g., <literal>@RequestParam(value="id",
required=false)</literal>).</para> required=false)</literal>).</para>
<para>Type conversion is applied automatically if the target method parameter
type is not <classname>String</classname>.
See <xref linkend="mvc-ann-typeconversion"/>.</para>
</section> </section>
<section id="mvc-ann-requestbody"> <section id="mvc-ann-requestbody">
@ -1516,6 +1519,10 @@ public void displayHeaderInfo(<emphasis role="bold">@CookieValue("JSESSIONID")</
}</programlisting> }</programlisting>
<para>Type conversion is applied automatically if the target method
parameter type is not <classname>String</classname>.
See <xref linkend="mvc-ann-typeconversion"/>.</para>
<para>This annotation is supported for annotated handler methods in <para>This annotation is supported for annotated handler methods in
Servlet and Portlet environments.</para> Servlet and Portlet environments.</para>
</section> </section>
@ -1550,10 +1557,40 @@ public void displayHeaderInfo(<emphasis role="bold">@RequestHeader("Accept-Encod
}</programlisting> }</programlisting>
<para>Type conversion is applied automatically if the method parameter
is not <classname>String</classname>.
See <xref linkend="mvc-ann-typeconversion"/>.</para>
<tip><para>Built-in support is available for converting a coma-separated
string into an array/collection of strings or other types known to the
type conversion system. For example a method parameter annotated with
<literal>@RequestHeader("Accept")</literal> may be of type
<classname>String</classname> but also <classname>String[]</classname>
or <classname>List&lt;String&gt;</classname>.</para></tip>
<para>This annotation is supported for annotated handler methods in <para>This annotation is supported for annotated handler methods in
Servlet and Portlet environments.</para> Servlet and Portlet environments.</para>
</section> </section>
<section id="mvc-ann-typeconversion">
<title>Method Parameters And Type Conversion</title>
<para>String-based values extracted from the request including
request parameters, path variables, request headers, and cookie values
may need to be converted to the target type of the method parameter or
field (e.g. binding a request parameter to a field in an
<interfacename>@ModelAttribute</interfacename> parameter) they're bound to.
If the target type is not <classname>String</classname>, Spring
automatically converts to the appropriate type.
All simple types such as int, long, Date, etc. are
supported. You can further customize the conversion process
through a <classname>WebDataBinder</classname>
(see <xref linkend="mvc-ann-webdatabinder" />) or by registering
<classname>Formatters</classname> with the
<classname>FormattingConversionService</classname>
(see <xref linkend="format" />).</para>
</section>
<section id="mvc-ann-webdatabinder"> <section id="mvc-ann-webdatabinder">
<title>Customizing <classname>WebDataBinder</classname> <title>Customizing <classname>WebDataBinder</classname>
initialization</title> initialization</title>