mvc.xml 148.0 KB
Newer Older
1
<?xml version="1.0" encoding="UTF-8"?>
2 3
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
4
<chapter id="mvc">
5 6 7
  <title>Web MVC framework</title>

  <section id="mvc-introduction">
8
    <title>Introduction to Spring Web MVC framework</title>
9

10 11 12 13
    <para>The Spring Web model-view-controller (MVC) framework is designed
    around a <classname>DispatcherServlet</classname> that dispatches requests
    to handlers, with configurable handler mappings, view resolution, locale
    and theme resolution as well as support for uploading files. The default
14 15 16 17
    handler is based on the <interfacename>@Controller</interfacename> and
    <interfacename>@RequestMapping</interfacename> annotations, offering a
    wide range of flexible handling methods. With the introduction of Spring
    3.0, the <interfacename>@Controller</interfacename> mechanism also allows
18
    you to create RESTful Web sites and applications, through the
J
Juergen Hoeller 已提交
19
    <interfacename>@PathVariable</interfacename> annotation and other
20
    features.</para>
21 22 23 24

    <sidebar id="mvc-open-for-extension">
      <title><quote>Open for extension...</quote></title>

25 26 27
      <para>A key design principle in Spring Web MVC and in Spring in general
      is the <quote><emphasis>Open for extension, closed for
      modification</emphasis></quote> principle.</para>
28

29 30
      <para>Some methods in the core classes of Spring Web MVC are marked
      <literal>final</literal>. As a developer you cannot override these
A
Arjen Poutsma 已提交
31 32
      methods to supply your own behavior. This has not been done arbitrarily,
      but specifically with this principal in mind.</para>
33

34 35 36 37
      <para>For an explanation of this principle, refer to <emphasis>Expert
      Spring Web MVC and Web Flow</emphasis> by Seth Ladd and others;
      specifically see the section "A Look At Design," on page 117 of the
      first edition. Alternatively, see</para>
38 39 40 41 42 43 44 45 46

      <orderedlist>
        <listitem>
          <para><ulink
          url="http://www.objectmentor.com/resources/articles/ocp.pdf">Bob
          Martin, The Open-Closed Principle (PDF)</ulink></para>
        </listitem>
      </orderedlist>

47 48
      <para>You cannot add advice to final methods when you use Spring MVC.
      For example, you cannot add advice to the
C
Costin Leau 已提交
49
      <literal>AbstractController.setSynchronizeOnSession()</literal> method. Refer to
50
      <xref linkend="aop-understanding-aop-proxies" /> for more information on
51 52 53
      AOP proxies and why you cannot add advice to final methods.</para>
    </sidebar>

54 55
    <para>In Spring Web MVC you can use any object as a command or
    form-backing object; you do not need to implement a framework-specific
56 57
    interface or base class. Spring's data binding is highly flexible: for
    example, it treats type mismatches as validation errors that can be
58 59 60 61 62
    evaluated by the application, not as system errors. Thus you need not
    duplicate your business objects' properties as simple, untyped strings in
    your form objects simply to handle invalid submissions, or to convert the
    Strings properly. Instead, it is often preferable to bind directly to your
    business objects.</para>
63 64

    <para>Spring's view resolution is extremely flexible. A
65
    <interfacename>Controller</interfacename> implementation can even write
66
    directly to the response stream. Typically, a
67 68
    <classname>ModelAndView</classname> instance consists of a view name and a
    model <interfacename>Map</interfacename>, which contains bean names and
69 70 71 72 73 74 75 76
    corresponding objects such as a command or form, which contain reference
    data. View name resolution is highly configurable, through bean names, a
    properties file, or your own <interfacename>ViewResolver</interfacename>
    implementation. The model (the M in MVC) is based on the
    <interfacename>Map</interfacename> interface, which allows for the
    complete abstraction of the view technology. You can integrate directly
    JSP, Velocity, or any other rendering technology. The model
    <interfacename>Map</interfacename> is simply transformed into an
77 78 79 80
    appropriate format, such as JSP request attributes or a Velocity template
    model.</para>

    <section id="mvc-features">
81 82 83
      <title>Features of Spring Web MVC<!--I moved Features of Spring Web MVC before Pluggability of other MVC implementations. You want to highlight your own imp. first.--></title>

      <!--Second line of sidebar refers to JSF; don't you mean JSP? Other refs in this context are to JSP. Also note, sidebar is read-only.-->
84

85 86
      <xi:include href="swf-sidebar.xml"
                  xmlns:xi="http://www.w3.org/2001/XInclude" />
87

88 89
      <para>Spring's web module includes many unique web support
      features:</para>
90 91 92

      <itemizedlist>
        <listitem>
93 94
          <para><emphasis>Clear separation of roles</emphasis>. Each role --
          controller, validator, command object, form object, model object,
95
          <classname>DispatcherServlet</classname>, handler mapping, view
96
          resolver, and so on -- can be fulfilled by a specialized
97 98 99 100
          object.</para>
        </listitem>

        <listitem>
101 102 103 104 105
          <para><emphasis>Powerful and straightforward configuration of both
          framework and application classes as JavaBeans</emphasis>. This
          configuration capability includes easy referencing across contexts,
          such as from web controllers to business objects and
          validators.</para>
106 107 108
        </listitem>

        <listitem>
109 110 111 112 113
          <para><emphasis>Adaptability, non-intrusiveness, and
          flexibility.</emphasis> Define any controller method signature you
          need, possibly using one of the parameter annotations (such as
          @RequestParam, @RequestHeader, @PathVariable, and more) for a given
          scenario.</para>
114 115 116
        </listitem>

        <listitem>
117 118 119 120
          <para><emphasis>Reusable business code</emphasis>,<emphasis> no need
          for duplication</emphasis>. Use existing business objects as command
          or form objects instead of mirroring them to extend a particular
          framework base class.</para>
121 122 123
        </listitem>

        <listitem>
124 125 126 127 128
          <para><emphasis>Customizable binding and validation</emphasis>. Type
          mismatches as application-level validation errors that keep the
          offending value, localized date and number binding, and so on
          instead of String-only form objects with manual parsing and
          conversion to business objects.</para>
129 130 131
        </listitem>

        <listitem>
132 133 134 135 136 137
          <para><emphasis>Customizable handler mapping and view
          resolution</emphasis>. Handler mapping and view resolution
          strategies range from simple URL-based configuration, to
          sophisticated, purpose-built resolution strategies. Spring is more
          flexible than web MVC frameworks that mandate a particular
          technique.</para>
138 139 140
        </listitem>

        <listitem>
141 142 143
          <para><emphasis>Flexible model transfer</emphasis>. Model transfer
          with a name/value <interfacename>Map</interfacename> supports easy
          integration with any view technology.</para>
144 145 146
        </listitem>

        <listitem>
147 148 149 150
          <para><emphasis>Customizable locale and theme resolution, support
          for JSPs with or without Spring tag library, support for JSTL,
          support for Velocity without the need for extra bridges, and so
          on.</emphasis></para>
151 152 153
        </listitem>

        <listitem>
154 155 156 157 158 159
          <para><emphasis>A simple yet powerful JSP tag library known as the
          Spring tag library that provides support for features such as data
          binding and themes</emphasis>. The custom tags allow for maximum
          flexibility in terms of markup code. For information on the tag
          library descriptor, see the appendix entitled <xref
          linkend="spring.tld" /></para>
160 161 162
        </listitem>

        <listitem>
163
          <para><emphasis>A JSP form tag library, introduced in Spring 2.0,
A
Arjen Poutsma 已提交
164
          that makes writing forms in JSP pages much easier.</emphasis> For
165 166
          information on the tag library descriptor, see the appendix entitled
          <xref linkend="spring-form.tld" /></para>
167 168 169
        </listitem>

        <listitem>
170 171 172 173 174 175
          <para><emphasis>Beans whose lifecycle is scoped to the current HTTP
          request or HTTP <interfacename>Session</interfacename>.</emphasis>
          This is not a specific feature of Spring MVC itself, but rather of
          the <interfacename>WebApplicationContext</interfacename>
          container(s) that Spring MVC uses. These bean scopes are described
          in <xref linkend="beans-factory-scopes-other" /></para>
176 177 178
        </listitem>
      </itemizedlist>
    </section>
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    <section id="mvc-introduction-pluggability">
      <title>Pluggability of other MVC implementations</title>

      <para>Non-Spring MVC implementations are preferable for some projects.
      Many teams expect to leverage their existing investment in skills and
      tools. A large body of knowledge and experience exist for the Struts
      framework. If you can abide Struts' architectural flaws, it can be a
      viable choice for the web layer; the same applies to WebWork and other
      web MVC frameworks.</para>

      <para>If you do not want to use Spring's web MVC, but intend to leverage
      other solutions that Spring offers, you can integrate the web MVC
      framework of your choice with Spring easily. Simply start up a Spring
      root application context through its
      <classname>ContextLoaderListener</classname>, and access it through
      its<!--Identify *its*. do you mean root application context's?-->
      <interfacename>ServletContext</interfacename> attribute (or Spring's
      respective helper method) from within a Struts or WebWork action. No
      "plug-ins" are involved, so no dedicated integration is necessary. From
      the web layer's point of view, you simply use Spring as a library, with
      the root application context instance as the entry point.</para>

      <para>Your registered beans and Spring's services can be at your
      fingertips even without Spring's Web MVC. Spring does not compete with
      Struts or WebWork in this scenario. It simply addresses the many areas
      that the pure web MVC frameworks do not, from bean configuration to data
      access and transaction handling. So you can enrich your application with
      a Spring middle tier and/or data access tier, even if you just want to
      use, for example, the transaction abstraction with JDBC or
      Hibernate.</para>
    </section>
211 212 213 214 215 216 217
  </section>

  <section id="mvc-servlet">
    <title>The <classname>DispatcherServlet</classname></title>

    <para>Spring's web MVC framework is, like many other web MVC frameworks,
    request-driven, designed around a central servlet that dispatches requests
218 219 220 221 222
    to controllers and offers other functionality that facilitates the
    development of web applications. Spring's
    <classname>DispatcherServlet</classname> however, does more than just
    that. It is completely integrated with the Spring IoC container and as
    such allows you to use every other feature that Spring has.</para>
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

    <para>The request processing workflow of the Spring Web MVC
    <classname>DispatcherServlet</classname> is illustrated in the following
    diagram. The pattern-savvy reader will recognize that the
    <classname>DispatcherServlet</classname> is an expression of the
    <quote>Front Controller</quote> design pattern (this is a pattern that
    Spring Web MVC shares with many other leading web frameworks).</para>

    <para><mediaobject>
        <imageobject role="fo">
          <imagedata align="center" fileref="images/mvc.png" format="PNG" />
        </imageobject>

        <imageobject role="html">
          <imagedata align="center" fileref="images/mvc.png" format="PNG" />
        </imageobject>

        <caption><para>The requesting processing workflow in Spring Web MVC
        (high level)</para></caption>
      </mediaobject></para>

244 245
    <para>The <classname>DispatcherServlet</classname> is an actual
    <interfacename>Servlet</interfacename> (it inherits from the
246
    <classname>HttpServlet</classname> base class), and as such is declared in
247 248 249 250 251 252
    the <literal>web.xml</literal> of your web application. You need to map
    requests that you want the <classname>DispatcherServlet</classname> to
    handle, by using a URL mapping in the same <literal>web.xml</literal>
    file. This is standard J2EE servlet configuration; the following example
    shows such a <classname>DispatcherServlet</classname> declaration and
    mapping:</para>
253

254
    <programlisting language="xml">&lt;web-app&gt;
255 256 257 258 259 260 261 262 263 264 265 266 267 268

    &lt;servlet&gt;
        &lt;servlet-name&gt;example&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;example&lt;/servlet-name&gt;
        &lt;url-pattern&gt;*.form&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;

&lt;/web-app&gt;</programlisting>

269 270 271 272 273 274 275
    <para>In the preceding example, all requests ending with
    <literal>.form</literal> will be handled by the <literal>example</literal>
    <classname>DispatcherServlet</classname>. This is only the first step in
    setting up Spring Web MVC. <!--The discussion below is a little vague about what you're doing, when you do it, and what you're accomplishing. --><!-- Is the next step shown in the next example screen?-->You
    now need to configure the various beans used by the Spring Web MVC
    framework (over and above the <classname>DispatcherServlet</classname>
    itself).<!--See previous sentence.Add info to indicate where you find info that tells you how to configure beans for MVC framework. --><!--Next paragraph, so what are you telling them to *do* here? --></para>
276

277
    <para>As detailed in <xref linkend="context-introduction" />,
278
    <interfacename>ApplicationContext</interfacename> instances in Spring can
279
    be scoped. In the Web MVC framework, each
280 281 282 283
    <classname>DispatcherServlet</classname> has its own
    <interfacename>WebApplicationContext</interfacename>, which inherits all
    the beans already defined in the root
    <interfacename>WebApplicationContext</interfacename>. These inherited
A
Arjen Poutsma 已提交
284 285
    beans can be overridden in the servlet-specific scope, and you can define
    new scope-specific beans local to a given servlet instance.</para>
286 287 288 289 290 291 292 293 294 295 296 297

    <para><mediaobject>
        <imageobject role="fo">
          <imagedata align="center" fileref="images/mvc-contexts.gif"
                     format="GIF" />
        </imageobject>

        <imageobject role="html">
          <imagedata align="center" fileref="images/mvc-contexts.gif"
                     format="GIF" />
        </imageobject>

298
        <caption>Context hierarchy in Spring Web MVC</caption>
299 300
      </mediaobject></para>

301 302 303
    <para>Upon initialization of a <classname>DispatcherServlet</classname>,
    the framework <!--Spring MVC or Spring Framework?--><emphasis><emphasis>looks
    for a file named</emphasis>
304
    <literal>[servlet-name]-servlet.xml</literal></emphasis> in the
305
    <literal>WEB-INF</literal> directory of your web application and creates
306 307
    the beans defined there, overriding the definitions of any beans defined
    with the same name in the global scope.</para>
308 309

    <para>Consider the following <classname>DispatcherServlet</classname>
310
    servlet configuration (in the <literal>web.xml</literal> file):</para>
311

312
    <programlisting language="xml">&lt;web-app&gt;
313 314 315 316 317 318 319 320 321

    &lt;servlet&gt;
        &lt;servlet-name&gt;<emphasis role="bold">golfing</emphasis>&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;<emphasis role="bold">golfing</emphasis>&lt;/servlet-name&gt;
322
        &lt;url-pattern&gt;/golfing/*&lt;/url-pattern&gt;
323 324 325 326
    &lt;/servlet-mapping&gt;

&lt;/web-app&gt;</programlisting>

327
    <para>With the above servlet configuration in place, <!--Is this something you need to do (in above example)? -->you
S
Sam Brannen 已提交
328
    will need to have a file called <literal>/WEB-INF/</literal><emphasis
A
Arjen Poutsma 已提交
329 330 331 332 333 334
    role="bold">golfing</emphasis><literal>-servlet.xml</literal> in your
    application; this file will contain all of your Spring Web MVC-specific
    components (beans). You can change the exact location of this
    configuration file through a servlet initialization parameter (see below
    for details).</para>

335
    <!--See *where* for details? Give x-ref to section talks about how to change the location of the file through servlet init. param.-->
336 337 338 339 340 341 342 343 344 345 346

    <para>The <interfacename>WebApplicationContext</interfacename> is an
    extension of the plain <interfacename>ApplicationContext</interfacename>
    that has some extra features necessary for web applications. It differs
    from a normal <interfacename>ApplicationContext</interfacename> in that it
    is capable of resolving themes (see <xref linkend="mvc-themeresolver" />),
    and that it knows which servlet it is associated with (by having a link to
    the <interfacename>ServletContext</interfacename>). The
    <interfacename>WebApplicationContext</interfacename> is bound in the
    <interfacename>ServletContext</interfacename>, and by using static methods
    on the <classname>RequestContextUtils</classname> class you can always
347 348 349 350 351 352 353 354 355
    look up the <interfacename>WebApplicationContext</interfacename> if you
    need access to it.</para>

    <para>The Spring <classname>DispatcherServlet</classname> uses special
    beans to process requests and render the appropriate views. These beans
    are part of Spring Framework. You can configure them in the
    <interfacename>WebApplicationContext</interfacename>, just as you
    configure any other bean. However, for most beans, sensible defaults are
    provided so you initially do not need to configure them. <!--Which beans have defaults? What do you mean you *initially* don't need to configure them? What determines whether you need to and --><!--when, if not *initially*? In table below, indicate which are defaults, which have to be configured.-->These
A
Arjen Poutsma 已提交
356
    beans are described in the following table.</para>
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

    <table id="mvc-webappctx-special-beans-tbl">
      <title>Special beans in the
      <interfacename>WebApplicationContext</interfacename></title>

      <tgroup cols="2">
        <colspec colname="c1" colwidth="1*" />

        <colspec colname="c2" colwidth="4*" />

        <thead>
          <row>
            <entry>Bean type</entry>

            <entry>Explanation</entry>
          </row>
        </thead>

        <tbody>
          <row>
377
            <entry><link linkend="mvc-controller">controllers</link></entry>
378

379
            <entry>Form the <literal>C</literal> part of the MVC.<!--Need info about controller function as with others in this list.Reader knows what C stands for.--></entry>
380 381 382
          </row>

          <row>
383 384
            <entry><link linkend="mvc-handlermapping">handler
            mappings</link></entry>
385

386 387 388 389
            <entry>Handle the execution of a list of pre-processors and
            post-processors and controllers that will be executed if they
            match certain criteria (for example, a matching URL specified with
            the controller).</entry>
390 391 392
          </row>

          <row>
393 394
            <entry><link linkend="mvc-viewresolver">view
            resolvers</link></entry>
395

396
            <entry>Resolves view names to views.<!--If it's capable of resolving, just say *resolves*. Like above, handler mappings are capable of handling the execution, but you just say *handle the execution*--></entry>
397 398 399
          </row>

          <row>
A
Arjen Poutsma 已提交
400
            <entry><link linkend="mvc-localeresolver">locale
401
            resolver</link></entry>
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

            <entry>A <link linkend="mvc-localeresolver">locale resolver</link>
            is a component capable of resolving the locale a client is using,
            in order to be able to offer internationalized views</entry>
          </row>

          <row>
            <entry>Theme resolver</entry>

            <entry>A <link linkend="mvc-themeresolver">theme resolver</link>
            is capable of resolving themes your web application can use, for
            example, to offer personalized layouts</entry>
          </row>

          <row>
            <entry>multipart file resolver</entry>

419 420
            <entry>Contains functionality to process file uploads from HTML
            forms.<!--Here and next one, why not just say processes file uploads, maps executions instead of *contains functionality to*?--></entry>
421 422 423
          </row>

          <row>
424 425
            <entry><link linkend="mvc-exceptionhandlers">handler exception
            resolvers</link></entry>
426

A
Arjen Poutsma 已提交
427
            <entry>Contains functionality to map exceptions to views or
428
            implement other more complex exception handling code.</entry>
429 430 431 432 433
          </row>
        </tbody>
      </tgroup>
    </table>

434 435 436 437 438
    <para>After you set up a <classname>DispatcherServlet</classname>, and a
    request comes in for that specific
    <classname>DispatcherServlet</classname>, the
    <classname>DispatcherServlet</classname> starts processing the request as
    follows:</para>
439 440 441 442

    <orderedlist>
      <listitem>
        <para>The <interfacename>WebApplicationContext</interfacename> is
443 444 445
        searched for and bound in the request as an attribute that the
        controller and other elements in the process can use. <!--Use to do *what*? Also revise to indicate *what* searches for the WebApplicationContext -->It
        is bound by default under the key
446 447 448 449
        <literal>DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE</literal>.</para>
      </listitem>

      <listitem>
450 451 452 453
        <para>The locale resolver is bound to the request to enable elements
        in the process to resolve the locale to use when processing the
        request (rendering the view, preparing data, and so on). If you do not
        need locale resolving, you do not need it.</para>
A
Arjen Poutsma 已提交
454
        <!--Reword 'if you don't need local resolving, you dont need to use it '. Are you saying locale resolving is optional? If you don't configure it, will this step occur?-->
455 456 457 458
      </listitem>

      <listitem>
        <para>The theme resolver is bound to the request to let elements such
459 460
        as views determine which theme to use. If you do not use themes, you
        can ignore it.</para>
A
Arjen Poutsma 已提交
461 462 463 464

        <!-- MLP perhaps say that there are not side effect to this binding.etc... Clarify *ignore it*. Does this step still occur if you don't use themes? -->

        <!--And what if you DO use themes, what do you do and when? Same question re locale resolving.-->
465 466 467
      </listitem>

      <listitem>
468 469 470 471 472 473
        <para>If you specify a multipart file resolver, the request is
        inspected for multiparts; if multiparts are found, the request is
        wrapped in a <classname>MultipartHttpServletRequest</classname> for
        further processing by other elements in the process. (See <xref
        linkend="mvc-multipart-resolver" /> for further information about
        multipart handling).</para>
474 475 476 477 478
      </listitem>

      <listitem>
        <para>An appropriate handler is searched for. If a handler is found,
        the execution chain associated with the handler (preprocessors,
479 480
        postprocessors, and controllers) is executed in order to prepare a
        model or rendering.</para>
481 482 483 484
      </listitem>

      <listitem>
        <para>If a model is returned, the view is rendered. If no model is
A
Arjen Poutsma 已提交
485 486 487 488 489
        returned, (may be due to a preprocessor or postprocessor intercepting
        the request, perhaps for security reasons), no view is rendered,
        because the request could already have been fulfilled.</para>

        <!--fulfilled how and by what?-->
490 491 492
      </listitem>
    </orderedlist>

493 494 495 496 497
    <para>Handler exception resolvers that are declared in the
    <interfacename>WebApplicationContext</interfacename> pick up exceptions
    that are thrown during processing of the request. Using these exception
    resolvers allows you to define custom behaviors to address
    exceptions.</para>
498

499 500
    <para>The Spring <classname>DispatcherServlet</classname> also supports
    the return of the <emphasis>last-modification-date</emphasis>, as
501 502
    specified by the Servlet API. The process of determining the last
    modification date for a specific request is straightforward: the
503
    <classname>DispatcherServlet</classname> looks up an appropriate handler
A
Arjen Poutsma 已提交
504
    mapping and tests whether the handler that is found implements the
505
    <emphasis><interfacename>LastModified</interfacename></emphasis>
506 507 508 509 510
    interface. If so, the value of the <literal>long
    getLastModified(request)</literal> method of the
    <interfacename>LastModified</interfacename> interface is returned to the
    client.</para>

A
Arjen Poutsma 已提交
511 512 513 514 515 516
    <para>You can customize individual
    <classname>DispatcherServlet</classname> instances by adding servlet
    initialization parameters (<literal>init-param</literal> elements) to the
    servlet declaration in the <literal>web.xml</literal> file. See the
    following table for the list of supported parameters.</para>

517 518
    <!--Reword above sentence to specify whether configuring parameters in table configures last-modification-date, or are they further -->
    <!--customization for some other purpose? If so, need to explain how you config last-modification-date-->
519 520 521 522 523 524 525

    <table id="mvc-disp-servlet-init-params-tbl">
      <title><classname>DispatcherServlet</classname> initialization
      parameters</title>

      <tgroup cols="2">
        <colspec colname="c1" colwidth="1*" />
526

527 528 529 530 531
        <colspec colname="c2" colwidth="4*" />

        <thead>
          <row>
            <entry>Parameter</entry>
532

533 534 535 536 537 538 539 540 541
            <entry>Explanation</entry>
          </row>
        </thead>

        <tbody>
          <row>
            <entry><literal>contextClass</literal></entry>

            <entry>Class that implements
542 543 544
            <interfacename>WebApplicationContext</interfacename>, which
            instantiates the context used by this servlet. By default, the
            <classname>XmlWebApplicationContext</classname> is used.</entry>
545 546 547 548 549
          </row>

          <row>
            <entry><literal>contextConfigLocation</literal></entry>

550 551 552 553 554 555
            <entry>String that is passed to the context instance (specified by
            <literal>contextClass</literal>) to indicate where context(s) can
            be found. The string consists potentially of multiple strings
            (using a comma as a delimiter) to support multiple contexts. In
            case of multiple context locations with beans that are defined
            twice, the latest location takes precedence.</entry>
S
Sam Brannen 已提交
556
            <!-- MLP review -->
557 558 559 560 561
          </row>

          <row>
            <entry><literal>namespace</literal></entry>

562
            <entry>Namespace of the
563 564 565 566 567 568 569 570 571
            <interfacename>WebApplicationContext</interfacename>. Defaults to
            <literal>[servlet-name]-servlet</literal>.</entry>
          </row>
        </tbody>
      </tgroup>
    </table>
  </section>

  <section id="mvc-controller">
572
    <title>Implementing Controllers</title>
573

574 575 576 577 578
    <para>Controllers provide access to the application behavior that you
    typically define through a service interface. <!--I changed preceding to active voice because next sentence refers to user input. Thus *you* do some defining.-->Controllers
    interpret user input and transform it into a model that is represented to
    the user by the view. Spring implements a controller in a very abstract
    way, which enables you to create a wide variety of controllers.</para>
579

580
    <para>Spring 2.5 introduced an annotation-based programming model for MVC
581
    controllers that uses annotations such as
582 583
    <interfacename>@RequestMapping</interfacename>,
    <interfacename>@RequestParam</interfacename>,
584
    <interfacename>@ModelAttribute</interfacename>, and so on. This annotation
585 586 587
    support is available for both Servlet MVC and Portlet MVC. Controllers
    implemented in this style do not have to extend specific base classes or
    implement specific interfaces. Furthermore, they do not usually have
588 589
    direct dependencies on Servlet or Portlet APIs, although you can easily
    configure access to Servlet or Portlet facilities.</para>
590

591 592
    <tip>
      <para>The Spring distribution ships with the
593 594 595
      <emphasis>PetClinic</emphasis> sample, a web application that leverages
      the annotation support described in this section, in the context of
      simple form processing. The <emphasis>PetClinic</emphasis> application
A
Arjen Poutsma 已提交
596 597
      resides in the <literal>org.springframework.samples.petclinic</literal>
      module.</para>
598 599

      <!-- MLP Note removed reference to imagedb -->
600
    </tip>
601

602 603
    <!--You need an intro sentence here that indicates the *purpose* of the following code.  -->

604 605
    <programlisting language="java">@Controller
public class HelloWorldController {
606

607 608
    @RequestMapping("/helloWorld")
    public ModelAndView helloWorld() {
S
Sam Brannen 已提交
609
        ModelAndView mav = new ModelAndView();
610 611 612 613
        mav.setViewName("helloWorld");
        mav.addObject("message", "Hello World!");
        return mav;
    }
614 615
}</programlisting>

616
    <para>As you can see, the <interfacename>@Controller</interfacename> and
617 618 619 620 621
    <interfacename>@RequestMapping</interfacename> annotations allow flexible
    method names and signatures. In this particular example the method has no
    parameters and returns a <classname>ModelAndView</classname>, but various
    other (and better) strategies exist, <!--strategies for doing *what*? -->as
    are explained later in this section. <classname>ModelAndView</classname>,
622 623
    <interfacename>@Controller</interfacename>, and
    <interfacename>@RequestMapping</interfacename> form the basis for the
624 625
    Spring MVC implementation. This section documents these annotations and
    how they are most commonly used in a Servlet environment.</para>
626

627 628 629
    <section id="mvc-ann-controller">
      <title>Defining a controller with
      <interfacename>@Controller</interfacename></title>
630

631 632
      <para>The <interfacename>@Controller</interfacename> annotation
      indicates that a particular class serves the role of a
633 634
      <emphasis>controller</emphasis>. Spring does not require you to extend
      any controller base class or reference the Servlet API. However, you can
S
Sam Brannen 已提交
635
      still reference Servlet-specific features if you need to.</para>
636 637 638 639

      <para>The <interfacename>@Controller</interfacename> annotation acts as
      a stereotype for the annotated class, indicating its role. The
      dispatcher scans such annotated classes for mapped methods and detects
640 641 642
      <interfacename>@RequestMapping</interfacename> annotations (see the next
      section).</para>

643
      <para>You can define annotated controller beans explicitly, using a
644 645 646 647 648
      standard Spring bean definition in the dispatcher's context. However,
      the <interfacename>@Controller</interfacename> stereotype also allows
      for autodetection, aligned with Spring general support for detecting
      component classes in the classpath and auto-registering bean definitions
      for them.</para>
A
Arjen Poutsma 已提交
649
      <!-- MLP Bev.changed to 'also supports autodetection -->
650 651 652 653
      <para>To enable autodetection of such annotated controllers, you add
      component scanning to your configuration. Use the
      <emphasis>spring-context</emphasis> schema as shown in the following XML
      snippet:</para>
654

655 656 657 658 659 660 661 662 663 664
      <programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
665

666
    &lt;context:component-scan base-package="org.springframework.samples.petclinic.web"/&gt;
667

668
    <lineannotation>// ...</lineannotation>
669

670 671
&lt;/beans&gt;</programlisting>
    </section>
672

673 674 675
    <section id="mvc-ann-requestmapping">
      <title>Mapping requests with
      <interfacename>@RequestMapping</interfacename></title>
676

677 678
      <para>You use the <interfacename>@RequestMapping</interfacename>
      annotation to map URLs such as <filename>/appointments</filename> onto
A
Arjen Poutsma 已提交
679 680 681 682 683
      an entire class or a particular handler method. Typically the
      class-level annotation maps a specific request path (or path pattern)
      onto a form controller, with additional method-level annotations
      narrowing the primary mapping for a specific HTTP method request method
      ("GET"/"POST") or specific HTTP request parameters.</para>
684

A
Arjen Poutsma 已提交
685 686
      <para>The following example shows a controller in a Spring MVC
      application that uses this annotation:</para>
687

688 689 690
      <programlisting language="java">@Controller
<emphasis role="bold">@RequestMapping("/appointments")</emphasis>
public class AppointmentsController {
691

S
Sam Brannen 已提交
692
    private final AppointmentBook appointmentBook;
693 694 695 696 697
    
    @Autowired
    public AppointmentsController(AppointmentBook appointmentBook) {
        this.appointmentBook = appointmentBook;
    }
698

699
    <emphasis role="bold">@RequestMapping(method = RequestMethod.GET)</emphasis>
700
    public Map&lt;String, Appointment&gt; get() {
701 702
        return appointmentBook.getAppointmentsForToday();
    }
703

704
    <emphasis role="bold">@RequestMapping(value="/{day}", method = RequestMethod.GET)</emphasis>
705 706
    public Map&lt;String, Appointment&gt; getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
        return appointmentBook.getAppointmentsForDay(day);
707
    }
708

709 710 711 712
    <emphasis role="bold">@RequestMapping(value="/new", method = RequestMethod.GET)</emphasis>
    public AppointmentForm getNewForm() {
        return new AppointmentForm();
    }
713

714
    <emphasis role="bold">@RequestMapping(method = RequestMethod.POST)</emphasis>
715 716 717 718 719
    public String add(@Valid AppointmentForm appointment, BindingResult result) {
        if (result.hasErrors()) {
            return "appointments/new";
        }
        appointmentBook.addAppointment(appointment);
720 721 722 723
        return "redirect:/appointments";
    }
}</programlisting>

724 725 726 727
      <para>In the example, the <interfacename>@RequestMapping</interfacename>
      is used in a number of places. The first usage is on the type (class)
      level, which indicates that all handling methods on this controller are
      relative to the <filename>/appointments</filename> path. The
728 729 730
      <methodname>get()</methodname> method has a further
      <interfacename>@RequestMapping</interfacename> refinement: it only
      accepts GET requests, meaning that an HTTP GET for
731 732 733 734
      <filename>/appointments</filename> invokes this method. The
      <methodname>post()</methodname> has a similar refinement, and the
      <methodname>getNewForm()</methodname> combines the definition of HTTP
      method and path into one, so that GET requests for
735 736 737 738
      <filename>appointments/new</filename> are handled by that method.</para>

      <para>The <methodname>getForDay()</methodname> method shows another
      usage of <interfacename>@RequestMapping</interfacename>: URI templates.
739 740
      (See <link linkend="mvc-ann-requestmapping-uri-templates">the next
      section </link>).</para>
741 742 743

      <para>A <interfacename>@RequestMapping</interfacename> on the class
      level is not required. Without it, all paths are simply absolute, and
744 745
      not relative. The following example from the PetClinic sample
      application shows a multi-action controller using
746 747 748 749
      <classname>@RequestMapping</classname>:</para>

      <programlisting language="java">@Controller
public class ClinicController {
750

751
    private final Clinic clinic;
752

753 754 755 756
    @Autowired
    public ClinicController(Clinic clinic) {
        this.clinic = clinic;
    }
757

758 759 760 761 762 763 764
    <emphasis role="bold">@RequestMapping("/")</emphasis>
    public void welcomeHandler() {
    }

    <emphasis role="bold">@RequestMapping("/vets")</emphasis>
    public ModelMap vetsHandler() {
        return new ModelMap(this.clinic.getVets());
765
    }
766

767 768
}</programlisting>

769 770
      <section id="mvc-ann-requestmapping-uri-templates">
        <title>URI Templates</title>
771

772 773
        <para>To access parts of a request URL in your handling methods, use
        the <emphasis><emphasis>URI templates</emphasis></emphasis> in the
774
        <interfacename>@RequestMapping</interfacename> path value.</para>
775

776 777
        <sidebar id="mvc-uri-templates">
          <title>URI Templates</title>
778

779
          <para>A URI Template is a URI-like string, containing one or more
780 781
          variable names. When you substitute values for these variables, the
          template becomes a URI. The <ulink
782
          url="http://bitworking.org/projects/URI-Templates/">proposed
783
          RFC</ulink> for URI Templates defines how a URI is parameterized.
784
          For example, the URI Template</para>
785

786
          <programlisting>http://www.example.com/users/{userid}</programlisting>
787

788 789
          <para>contains the variable <emphasis>userid</emphasis>. If we
          assign the variable the value fred, the URI Template yields:</para>
790

791
          <programlisting>http://www.example.com/users/fred</programlisting>
792

793 794
          <para>During the processing of a request, the URI can be compared to
          an expected URI Template in order to extract a collection of
795 796
          variables.</para>
        </sidebar>
797

S
Sam Brannen 已提交
798
        <para>Use the <interfacename>@PathVariable</interfacename> method
799 800
        parameter annotation to indicate that a method parameter should be
        bound to the value of a URI template variable.</para>
801

S
Sam Brannen 已提交
802
        <para>The following code snippet shows the usage of a single
803 804
        <interfacename>@PathVariable</interfacename> in a controller
        method:</para>
805

806 807 808 809 810 811 812
        <programlisting language="java">@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(<emphasis role="bold">@PathVariable</emphasis> String ownerId, Model model) {
  Owner owner = ownerService.findOwner(ownerId);  
  model.addAttribute("owner", owner);  
  return "displayOwner"; 
}
</programlisting>
813

814
        <para>The URI Template "<literal>/owners/{ownerId}</literal>"
815 816 817 818 819
        specifies the variable name <emphasis>ownerId</emphasis>. When the
        controller handles this request, the value of
        <emphasis>ownerId</emphasis> is set to the value in the request URI.
        For example, when a request comes in for /owners/fred, the value fred
        is bound to the method parameter <literal>String
820 821
        ownerId</literal>.</para>

S
Sam Brannen 已提交
822
        <!-- MLP: Bev Review -->
823 824 825
        <para>The matching of method parameter names to URI Template variable
        names can only be done if your code is compiled with debugging
        enabled. If you do have not debugging enabled, you must specify the
826
        name of the URI Template variable name in the @PathVariable annotation
A
Arjen Poutsma 已提交
827 828
        in order to bind the resolved value of the variable name to a method
        parameter. For example:</para>
829 830 831 832

        <programlisting language="java">@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(<emphasis role="bold">@PathVariable</emphasis>("ownerId") String ownerId, Model model) {
  // implementation omitted
S
Sam Brannen 已提交
833
}</programlisting>
A
Arjen Poutsma 已提交
834 835

        <para>You can also use a controller method with the following
836
        signature:</para>
837 838 839 840

        <programlisting language="java">@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(<emphasis role="bold">@PathVariable</emphasis>("ownerId") String theOwner, Model model) {
  // implementation omitted
841 842
}</programlisting>

843 844
        <para>You can use multiple @PathVariable annotations to bind <!--specify: to bind *what* to multiple URI template variables? method parameter String ownerId?-->to
        multiple URI Template variables:</para>
845

846 847 848 849 850 851 852 853 854
        <programlisting language="java">@RequestMapping(value="/owners/{ownerId}/pets/{petId}", method=RequestMethod.GET)
public String findPet(<emphasis role="bold">@PathVariable</emphasis> String ownerId, <emphasis
            role="bold">@PathVariable</emphasis> String petId, Model model) {
  Owner owner = ownerService.findOwner(ownderId);  
  Pet pet = owner.getPet(petId);  
  model.addAttribute("pet", pet);  
  return "displayPet"; 
}
</programlisting>
855

A
Arjen Poutsma 已提交
856 857
        <para>The following code snippet shows the usage of path variables on
        a relative path, so that the <methodname>findPet()</methodname> method
858 859
        will be invoked for <filename>/owners/42/pets/21</filename>, for
        instance.</para>
860

861 862 863
        <programlisting language="java">@Controller
@RequestMapping(<emphasis role="bold">"/owners/{ownerId}"</emphasis>)
public class RelativePathUriTemplateController {
864

865 866 867 868 869 870
  @RequestMapping(<emphasis role="bold">"/pets/{petId}"</emphasis>)
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
    // implementation omitted
  }
}
</programlisting>
871

872 873 874 875
        <tip>
          <para>Method parameters that are decorated with the
          <interfacename>@PathVariable</interfacename> annotation can be of
          <emphasis role="bold">any simple type </emphasis>such as int, long,
S
Sam Brannen 已提交
876
          Date, etc. Spring automatically converts to the appropriate type and
877
          throws a <classname>TypeMismatchException</classname> if the type is
878 879
          not correct. You can further customize this conversion process by
          customizing the data binder. See <xref
880 881 882
          linkend="mvc-ann-webdatabinder" />.</para>
        </tip>
      </section>
883

884 885 886 887 888 889
      <section id="mvc-ann-requestmapping-advanced">
        <title>Advanced <interfacename>@RequestMapping</interfacename>
        options</title>

        <para>In addition to URI templates, the
        <interfacename>@RequestMapping</interfacename> annotation also
890
        supports Ant-style path patterns (for example,
891
        <filename>/myPath/*.do</filename>). A combination of URI templates and
892
        Ant-style globs is also supported (for example,
893 894 895 896 897 898 899
        <filename>/owners/*/pets/{petId}</filename>).</para>

        <para>The handler method names are taken into account for narrowing if
        no path was specified explicitly, according to the specified
        <interfacename>org.springframework.web.servlet.mvc.multiaction.MethodNameResolver</interfacename>
        (by default an
        <classname>org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver</classname>).
900 901 902 903
        This only applies if annotation mappings do not specify a path mapping
        explicitly. In other words, the method name is only used for narrowing
        among a set of matching methods; it does not constitute a primary path
        mapping itself.</para>
904

905 906
        <para>If you have a single default method (without explicit path
        mapping), then all requests without a more specific mapped method
907 908 909
        found are dispatched to it. If you have multiple such default methods,
        then the method name is taken into account for choosing between
        them.</para>
910

911
        <para>You can narrow path mappings through parameter conditions: a
912 913 914 915 916 917 918 919 920 921 922 923 924
        sequence of "myParam=myValue" style expressions, with a request only
        mapped if each such parameter is found to have the given value. For
        example: <programlisting language="java">@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {

  @RequestMapping(value = "/pets/{petId}", <emphasis role="bold">params="myParam=myValue"</emphasis>)
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
    // implementation omitted
  }
}
</programlisting> "myParam" style expressions are also supported, with such
        parameters having to be present in the request (allowed to have any
925 926 927
        value). <!--I don't understand the preceding sentence. Can you reword?-->Finally,
        "!myParam" style expressions indicate that the specified parameter is
        <emphasis>not</emphasis> supposed to be present in the request.</para>
928 929

        <para>Similarly, path mappings can be narrowed down through header
S
Sam Brannen 已提交
930
        conditions:</para>
A
Arjen Poutsma 已提交
931

S
Sam Brannen 已提交
932
        <programlisting language="java">@Controller
933 934 935 936
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {

@RequestMapping(value = "/pets", method = RequestMethod.POST, <emphasis
A
Arjen Poutsma 已提交
937
            role="bold">headers="content-type=text/*"</emphasis>)
938 939 940
  public void addPet(Pet pet, @PathVariable String ownerId) {    
    // implementation omitted
  }
S
Sam Brannen 已提交
941 942
}</programlisting>

A
Arjen Poutsma 已提交
943 944 945 946
        <para>In the above example, the <methodname>addPet()</methodname>
        method is only invoked when the <literal>content-type</literal>
        matches the <literal>text/*</literal> pattern, for example,
        <literal>text/xml</literal>.</para>
947
      </section>
948

949 950 951
      <section id="mvc-ann-requestmapping-arguments">
        <title>Supported handler method arguments and return types</title>

952 953 954
        <para>Handler methods that are annotated with
        <classname>@RequestMapping</classname> can have very flexible
        signatures. They may have arguments of the following types, in
S
Sam Brannen 已提交
955
        arbitrary order (except for validation results, which need to follow
A
Arjen Poutsma 已提交
956
        right after the corresponding command object, if desired): <!--Reword preceding sentence to clarify, make it a complete sentence and no parentheses: first it says validation results *must*--><!--immediately follow command object, but then it says *if desired*. Clarify what must happen if what is desired. And are validation --><!-- results a type of argument? Relate to the sentence that precedes it.-->
957 958
        <itemizedlist>
            <listitem>
S
Sam Brannen 已提交
959 960 961
              <para>Request or response objects (Servlet API). Choose any
              specific request or response type, for example
              <interfacename>ServletRequest</interfacename> or
962 963 964 965 966 967
              <interfacename>HttpServletRequest</interfacename>.</para>
            </listitem>

            <listitem>
              <para>Session object (Servlet API): of type
              <interfacename>HttpSession</interfacename>. An argument of this
968 969
              type enforces the presence of a corresponding session. As a
              consequence, such an argument is never
970 971 972
              <literal>null</literal>.</para>

              <note>
973
                <para>Session access may not be thread-safe, in particular in
S
Sam Brannen 已提交
974
                a Servlet environment. Consider setting the
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
                <classname>AnnotationMethodHandlerAdapter</classname>'s
                "synchronizeOnSession" flag to "true" if multiple requests are
                allowed to access a session concurrently.</para>
              </note>
            </listitem>

            <listitem>
              <para><classname>org.springframework.web.context.request.WebRequest</classname>
              or
              <classname>org.springframework.web.context.request.NativeWebRequest</classname>.
              Allows for generic request parameter access as well as
              request/session attribute access, without ties to the native
              Servlet/Portlet API.</para>
            </listitem>

            <listitem>
              <para><classname>java.util.Locale</classname> for the current
992 993
              request locale, determined by the most specific locale resolver
              available, in effect, the configured
994
              <interfacename>LocaleResolver</interfacename> in a Servlet
995
              environment.</para>
996 997 998 999 1000
            </listitem>

            <listitem>
              <para><classname>java.io.InputStream</classname> /
              <classname>java.io.Reader</classname> for access to the
1001
              request's content. This value is the raw InputStream/Reader as
1002 1003 1004 1005 1006 1007
              exposed by the Servlet API.</para>
            </listitem>

            <listitem>
              <para><classname>java.io.OutputStream</classname> /
              <classname>java.io.Writer</classname> for generating the
1008
              response's content. This value is the raw OutputStream/Writer as
1009 1010 1011 1012
              exposed by the Servlet API.</para>
            </listitem>

            <listitem>
J
Juergen Hoeller 已提交
1013
              <para><classname>@PathVariable</classname> annotated parameters
1014
              for access to URI template variables. See <xref
1015 1016 1017 1018 1019 1020
              linkend="mvc-ann-requestmapping-uri-templates" />.</para>
            </listitem>

            <listitem>
              <para><classname>@RequestParam</classname> annotated parameters
              for access to specific Servlet request parameters. Parameter
1021 1022
              values are converted to the declared method argument type. See
              <xref linkend="mvc-ann-requestparam" />.</para>
1023 1024 1025 1026 1027
            </listitem>

            <listitem>
              <para><classname>@RequestHeader</classname> annotated parameters
              for access to specific Servlet request HTTP headers. Parameter
1028
              values are converted to the declared method argument
1029 1030 1031 1032 1033
              type.</para>
            </listitem>

            <listitem>
              <para><classname>@RequestBody</classname> annotated parameters
S
Sam Brannen 已提交
1034
              for access to the HTTP request body. Parameter values are
1035 1036 1037 1038 1039 1040 1041 1042 1043
              converted to the declared method argument type using
              <interfacename>HttpMessageConverter</interfacename>s. See <xref
              linkend="mvc-ann-requestbody" />.</para>
            </listitem>

            <listitem>
              <para><interfacename>java.util.Map</interfacename> /
              <interfacename>org.springframework.ui.Model</interfacename> /
              <classname>org.springframework.ui.ModelMap</classname> for
1044
              enriching the implicit model that is exposed to the web
1045 1046 1047 1048
              view.</para>
            </listitem>

            <listitem>
1049 1050 1051 1052 1053 1054
              <para>Command or form objects to bind parameters to: as bean
              properties or fields, <!--What do you mean by *as bean properties or fields*, what does that refer to? Why do you have a colon? Don't get this line.--><!--*to bind parameters to* is awkward. Avoid slashes as with *command/form objects*. Do you mean command or form objects? Revise.-->with
              customizable type conversion, depending on
              <classname>@InitBinder</classname> methods and/or the
              HandlerAdapter configuration. See the
              <literal>webBindingInitializer</literal> property on
1055 1056
              <classname>AnnotationMethodHandlerAdapter</classname>. Such
              command objects along with their validation results will be
S
Sam Brannen 已提交
1057
              exposed as model attributes by default, using the non-qualified
1058 1059 1060 1061
              command class name in property notation. <!--Who or what uses the non-qualified class name in property notation? Is this something you have to set up?-->For
              example, "orderAddress" for type "mypackage.OrderAddress".
              Specify a parameter-level <classname>ModelAttribute</classname>
              annotation for declaring a specific model attribute name.</para>
1062 1063 1064 1065 1066 1067
            </listitem>

            <listitem>
              <para><classname>org.springframework.validation.Errors</classname>
              /
              <classname>org.springframework.validation.BindingResult</classname>
1068
              validation results for a preceding command or form object (the
S
Sam Brannen 已提交
1069
              immediately preceding method argument).</para>
1070 1071 1072 1073
            </listitem>

            <listitem>
              <para><classname>org.springframework.web.bind.support.SessionStatus</classname>
1074 1075
              status handle for marking form processing as complete, which
              triggers the cleanup of session attributes that have been
1076
              indicated by the <classname>@SessionAttributes</classname>
1077
              annotation at the handler type level.</para>
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
            </listitem>
          </itemizedlist></para>

        <para>The following return types are supported for handler methods:
        <itemizedlist>
            <listitem>
              <para>A <classname>ModelAndView</classname> object, with the
              model implicitly enriched with command objects and the results
              of <literal>@ModelAttribute</literal> annotated reference data
              accessor methods.</para>
            </listitem>

            <listitem>
              <para>A <interfacename>Model</interfacename> object, with the
              view name implicitly determined through a
              <interfacename>RequestToViewNameTranslator</interfacename> and
              the model implicitly enriched with command objects and the
              results of <literal>@ModelAttribute</literal> annotated
              reference data accessor methods.</para>
            </listitem>

            <listitem>
              <para>A <interfacename>Map</interfacename> object for exposing a
              model, with the view name implicitly determined through a
              <interfacename>RequestToViewNameTranslator</interfacename> and
              the model implicitly enriched with command objects and the
              results of <literal>@ModelAttribute</literal> annotated
              reference data accessor methods.</para>
            </listitem>

            <listitem>
              <para>A <interfacename>View</interfacename> object, with the
              model implicitly determined through command objects and
              <literal>@ModelAttribute</literal> annotated reference data
              accessor methods. The handler method may also programmatically
              enrich the model by declaring a
1114
              <interfacename>Model</interfacename> argument (see above).<!--see above where? Need more explicit reference. same problem with next item.--></para>
1115 1116 1117
            </listitem>

            <listitem>
1118
              <para>A <classname>String</classname> value that is interpreted
A
Arjen Poutsma 已提交
1119 1120 1121 1122
              as the logical view name, with the model implicitly determined
              through command objects and <literal>@ModelAttribute</literal>
              annotated reference data accessor methods. The handler method
              may also programmatically enrich the model by declaring a
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
              <interfacename>Model</interfacename> argument (see
              above).</para>
            </listitem>

            <listitem>
              <para><literal>void</literal> if the method handles the response
              itself (by writing the response content directly, declaring an
              argument of type <interfacename>ServletResponse</interfacename>
              / <interfacename>HttpServletResponse</interfacename> for that
              purpose) or if the view name is supposed to be implicitly
              determined through a
              <interfacename>RequestToViewNameTranslator</interfacename> (not
              declaring a response argument in the handler method
              signature).</para>
            </listitem>

            <listitem>
              <para>If the method is annotated with
1141 1142 1143
              <interfacename>@ResponseBody</interfacename>, the return type is
              written to the response HTTP body. The return value will be
              converted to the declared method argument type using
1144 1145 1146 1147 1148
              <interfacename>HttpMessageConverter</interfacename>s. See <xref
              linkend="mvc-ann-responsebody" />.</para>
            </listitem>

            <listitem>
S
Sam Brannen 已提交
1149
              <para>Any other return type is considered to be a single model
1150 1151 1152
              attribute to be exposed to the view, using the attribute name
              specified through <literal>@ModelAttribute</literal> at the
              method level (or the default attribute name based on the return
1153 1154 1155
              type class name). The model is implicitly enriched with command
              objects and the results of <literal>@ModelAttribute</literal>
              annotated reference data accessor methods.</para>
1156 1157 1158
            </listitem>
          </itemizedlist></para>
      </section>
1159

1160 1161 1162
      <section id="mvc-ann-requestparam">
        <title>Binding request parameters to method parameters with
        <classname>@RequestParam</classname></title>
1163

1164 1165
        <para>Use the <classname>@RequestParam</classname> annotation to bind
        request parameters to a method parameter in your controller.</para>
1166

1167
        <para>The following code snippet shows the usage:</para>
1168

1169 1170 1171 1172
        <programlisting language="java">@Controller
@RequestMapping("/pets")
@SessionAttributes("pet")
public class EditPetForm {
1173

1174
    <lineannotation>// ...</lineannotation>
1175

1176 1177 1178 1179 1180 1181
    @RequestMapping(method = RequestMethod.GET)
    public String setupForm(<emphasis role="bold">@RequestParam("petId") int petId</emphasis>, ModelMap model) {
        Pet pet = this.clinic.loadPet(petId);
        model.addAttribute("pet", pet);
        return "petForm";
    }
1182

1183 1184
    <lineannotation>// ...</lineannotation>
</programlisting>
1185

1186 1187 1188 1189 1190
        <para>Parameters using this annotation are required by default, but
        you can specify that a parameter is optional by setting
        <interfacename>@RequestParam</interfacename>'s
        <literal>required</literal> attribute to <literal>false</literal>
        (e.g., <literal>@RequestParam(value="id",
S
Sam Brannen 已提交
1191
        required=false)</literal>).</para>
1192
      </section>
1193

1194 1195 1196
      <section id="mvc-ann-requestbody">
        <title>Mapping the request body with the @RequestBody
        annotation</title>
1197

1198
        <para>The <classname>@RequestBody</classname> method parameter
1199 1200
        annotation indicates that a method parameter should be bound to the
        value of the HTTP request body. For example:</para>
1201

1202 1203 1204 1205
        <programlisting language="java">@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
  writer.write(body);
}</programlisting>
1206

S
Sam Brannen 已提交
1207
        <para>You convert the request body to the method argument by using an
1208
        <interfacename>HttpMessageConverter</interfacename>.
1209 1210 1211 1212 1213 1214
        <interfacename>HttpMessageConverter</interfacename> is responsible for
        converting from the HTTP request message to an object and converting
        from an object to the HTTP response body.
        <classname>DispatcherServlet</classname> supports annotation based
        processing using the
        <classname>DefaultAnnotationHandlerMapping</classname> and
1215 1216 1217 1218 1219
        <classname>AnnotationMethodHandlerAdapter</classname>. In Spring 3.0
        the <classname>AnnotationMethodHandlerAdapter</classname> is extended
        to support the <classname>@RequestBody</classname> and has the
        following <interfacename>HttpMessageConverters</interfacename>
        registered by default:</para>
1220 1221 1222

        <itemizedlist>
          <listitem>
1223 1224
            <para><classname>ByteArrayHttpMessageConverter</classname>
            converts byte arrays.</para>
1225 1226 1227
          </listitem>

          <listitem>
1228 1229
            <para><classname>StringHttpMessageConverter</classname> converts
            strings.</para>
1230 1231 1232
          </listitem>

          <listitem>
1233 1234
            <para><classname>FormHttpMessageConverter</classname> converts
            form data to/from a MultiValueMap&lt;String, String&gt;.</para>
1235 1236 1237
          </listitem>

          <listitem>
1238 1239
            <para><classname>SourceHttpMessageConverter</classname> converts
            to/from a javax.xml.transform.Source.</para>
1240 1241 1242
          </listitem>

          <listitem>
1243
            <para><classname>MarshallingHttpMessageConverter</classname>
1244 1245 1246 1247 1248
            converts to/from an object using the
            <classname>org.springframework.oxm</classname> package.</para>
          </listitem>
        </itemizedlist>

1249 1250
        <para>For more information on these converters, see <link
        linkend="rest-message-conversion">Message Converters</link>.</para>
1251 1252 1253 1254 1255 1256 1257

        <para>The <classname>MarshallingHttpMessageConverter</classname>
        requires a <interfacename>Marshaller</interfacename> and
        <interfacename>Unmarshaller</interfacename> from the
        <classname>org.springframework.oxm</classname> package to be
        configured on an instance of
        <classname>AnnotationMethodHandlerAdapter</classname> in the
S
Sam Brannen 已提交
1258
        application context. For example:</para>
1259

1260 1261 1262 1263 1264 1265 1266 1267
        <programlisting language="xml">&lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
    &lt;property name="messageConverters"&gt;
      &lt;util:list id="beanList"&gt;
        &lt;ref bean="stringHttpMessageConverter"/&gt;
        &lt;ref bean="marshallingHttpMessageConverter"/&gt;
      &lt;/util:list&gt;
    &lt;/property
&lt;/bean&gt;
1268

1269 1270
&lt;bean id="stringHttpMessageConverter" 
       class="org.springframework.http.converter.StringHttpMessageConverter"/&gt;
1271

1272 1273 1274 1275 1276
&lt;bean id="marshallingHttpMessageConverter" 
      class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"&gt;
  &lt;property name="marshaller" ref="castorMarshaller" /&gt;
  &lt;property name="unmarshaller" ref="castorMarshaller" /&gt;
&lt;/bean&gt;
1277

1278 1279 1280
&lt;bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/&gt;
</programlisting>
      </section>
1281

1282
      <section id="mvc-ann-responsebody">
1283 1284
        <title>Mapping the response body with the @ResponseBody
        annotation</title>
1285

1286 1287 1288 1289 1290 1291
        <para>The <interfacename>@ResponseBody</interfacename> annotation is
        similar to <interfacename>@RequestBody</interfacename>. This
        annotation can be put on a method <!--Revise *can be put on*. You do *what* with this annotation in regard to a method?-->and
        indicates that the return type should be written straight to the HTTP
        response body (and not placed in a Model, or interpreted as a view
        name). For example:</para>
1292

1293 1294 1295 1296 1297
        <programlisting language="java">@RequestMapping(value = "/something", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld()  {
  return "Hello World";
}</programlisting>
1298

S
Sam Brannen 已提交
1299
        <para>The above example will result in the text <literal>Hello
1300
        World</literal> being written to the HTTP response stream.</para>
1301

A
Arjen Poutsma 已提交
1302 1303
        <para>As with <interfacename>@RequestBody</interfacename>, Spring
        converts the returned object to a response body by using an
1304 1305 1306
        <interfacename>HttpMessageConverter</interfacename>. For more
        information on these converters, see the previous section and <link
        linkend="rest-message-conversion">Message Converters</link>.</para>
1307
      </section>
1308

1309 1310 1311 1312 1313
      <section id="mvc-ann-modelattrib">
        <title>Providing a link to data from the model with
        <classname>@ModelAttribute</classname></title>

        <para><classname>@ModelAttribute</classname> has two usage scenarios
1314 1315
        in controllers. When you map it to <!--is this correct, *map it to*? If not, what do you mean by *is placed on*?-->a
        method parameter, <classname>@ModelAttribute</classname> maps a model
1316 1317 1318 1319 1320
        attribute to the specific, annotated method parameter (see the
        <literal>processSubmit()</literal> method below). This is how the
        controller gets a reference to the object holding the data entered in
        the form.</para>

A
Arjen Poutsma 已提交
1321 1322 1323 1324
        <para>You can also use <classname>@ModelAttribute</classname> at the
        method level to provide <emphasis>reference data</emphasis> for the
        model (see the <literal>populatePetTypes()</literal> method in the
        following example). For this usage the method signature can contain
1325 1326
        the same types as documented previously for the
        <classname>@RequestMapping</classname> annotation.</para>
1327 1328

        <note>
1329
          <para><classname>@ModelAttribute</classname> annotated methods are
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
          executed <emphasis>before</emphasis> the chosen
          <classname>@RequestMapping</classname> annotated handler method.
          They effectively pre-populate the implicit model with specific
          attributes, often loaded from a database. Such an attribute can then
          already be accessed through <classname>@ModelAttribute</classname>
          annotated handler method parameters in the chosen handler method,
          potentially with binding and validation applied to it.</para>
        </note>

        <para>The following code snippet shows these two usages of this
        annotation:</para>
1341

1342 1343 1344 1345
        <programlisting language="java">@Controller
@RequestMapping("/owners/{ownerId}/pets/{petId}/edit")
@SessionAttributes("pet")
public class EditPetForm {
1346

S
Sam Brannen 已提交
1347
    <lineannotation>// ...</lineannotation>
1348

S
Sam Brannen 已提交
1349 1350 1351 1352
    <emphasis role="bold">@ModelAttribute("types")</emphasis>
    public Collection&lt;PetType&gt; populatePetTypes() {
        return this.clinic.getPetTypes();
    }
1353

S
Sam Brannen 已提交
1354 1355 1356 1357
    @RequestMapping(method = RequestMethod.POST)
    public String processSubmit(
            <emphasis role="bold">@ModelAttribute("pet") Pet pet</emphasis>,
            BindingResult result, SessionStatus status) {
1358

S
Sam Brannen 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
        new PetValidator().validate(pet, result);
        if (result.hasErrors()) {
            return "petForm";
        }
        else {
            this.clinic.storePet(pet);
            status.setComplete();
            return "redirect:owner.do?ownerId=" + pet.getOwner().getId();
        }
    }
1369

1370 1371 1372 1373
}</programlisting>
      </section>

      <section id="mvc-ann-sessionattrib">
1374
        <title>Specifying attributes to store in a session with
1375 1376 1377 1378
        <classname>@SessionAttributes</classname></title>

        <para>The type-level <classname>@SessionAttributes</classname>
        annotation declares session attributes used by a specific handler.
S
Sam Brannen 已提交
1379
        This will typically list the names of model attributes or types of
A
Arjen Poutsma 已提交
1380 1381 1382
        model attributes which should be transparently stored in the session
        or some conversational storage, serving as form-backing beans between
        subsequent requests.</para>
1383

A
Arjen Poutsma 已提交
1384 1385
        <para>The following code snippet shows the usage of this annotation,
        specifying the model attribute name:</para>
1386 1387 1388 1389 1390 1391

        <programlisting language="java">@Controller
@RequestMapping("/editPet.do")
<emphasis role="bold">@SessionAttributes("pet")</emphasis>
public class EditPetForm {
    <lineannotation>// ...</lineannotation>
S
Sam Brannen 已提交
1392
}</programlisting>
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
      </section>

      <section id="mvc-ann-cookievalue">
        <title>Mapping cookie values with the @CookieValue annotation</title>

        <para>The <interfacename>@CookieValue</interfacename> annotation
        allows a method parameter to be bound to the value of an HTTP
        cookie.</para>

        <para>Let us consider that the following cookie has been received with
        an http request:</para>

        <programlisting>JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84</programlisting>

1407 1408
        <para>The following code sample demonstrates how to get the value of
        the <literal>JSESSIONID</literal> cookie:</para>
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427

        <programlisting language="java">@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(<emphasis role="bold">@CookieValue("JSESSIONID")</emphasis> String cookie)  {

  //...

}</programlisting>

        <para>This annotation is supported for annotated handler methods in
        Servlet and Portlet environments.</para>
      </section>

      <section id="mvc-ann-requestheader">
        <title>Mapping request header attributes with the @RequestHeader
        annotation</title>

        <para>The <interfacename>@RequestHeader</interfacename> annotation
        allows a method parameter to be bound to a request header.</para>

1428
        <para>Here is a sample request header:</para>
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438

        <programlisting>
Host                    localhost:8080
Accept                  text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language         fr,en-gb;q=0.7,en;q=0.3
Accept-Encoding         gzip,deflate
Accept-Charset          ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive              300
</programlisting>

1439
        <para>The following code sample demonstrates how to get the value of
A
Arjen Poutsma 已提交
1440 1441
        the <literal>Accept-Encoding</literal> and
        <literal>Keep-Alive</literal> headers:</para>
1442 1443

        <programlisting language="java">@RequestMapping("/displayHeaderInfo.do")
1444 1445
public void displayHeaderInfo(<emphasis role="bold">@RequestHeader("Accept-Encoding")</emphasis> String encoding,
                              <emphasis role="bold">@RequestHeader("Keep-Alive")</emphasis> long keepAlive)  {
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458

  //...

}</programlisting>

        <para>This annotation is supported for annotated handler methods in
        Servlet and Portlet environments.</para>
      </section>

      <section id="mvc-ann-webdatabinder">
        <title>Customizing <classname>WebDataBinder</classname>
        initialization</title>

1459 1460 1461
        <para>To customize request parameter binding with PropertyEditors
        through Spring's <classname>WebDataBinder</classname>, you can use
        either <interfacename>@InitBinder</interfacename>-annotated methods
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
        within your controller or externalize your configuration by providing
        a custom <interfacename>WebBindingInitializer</interfacename>.</para>

        <section id="mvc-ann-initbinder">
          <title>Customizing data binding with
          <interfacename>@InitBinder</interfacename></title>

          <para>Annotating controller methods with
          <interfacename>@InitBinder</interfacename> allows you to configure
          web data binding directly within your controller class.
1472
          <interfacename>@InitBinder</interfacename> identifies methods that
1473
          initialize the <classname>WebDataBinder</classname> that will be
1474
          used to populate command and form object arguments of annotated
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
          handler methods.</para>

          <para>Such init-binder methods support all arguments that
          <interfacename>@RequestMapping</interfacename> supports, except for
          command/form objects and corresponding validation result objects.
          Init-binder methods must not have a return value. Thus, they are
          usually declared as <literal>void</literal>. Typical arguments
          include <classname>WebDataBinder</classname> in combination with
          <interfacename>WebRequest</interfacename> or
          <classname>java.util.Locale</classname>, allowing code to register
          context-specific editors.</para>

          <para>The following example demonstrates the use of
1488
          <interfacename>@InitBinder</interfacename> to configure a
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
          <classname>CustomDateEditor</classname> for all
          <classname>java.util.Date</classname> form properties.</para>

          <programlisting language="java">@Controller
public class MyFormController {

    <emphasis role="bold">@InitBinder</emphasis>
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    <lineannotation>// ...</lineannotation>
}</programlisting>
        </section>

        <section id="mvc-ann-webbindinginitializer">
          <title>Configuring a custom
          <interfacename>WebBindingInitializer</interfacename></title>

          <para>To externalize data binding initialization, you can provide a
          custom implementation of the
          <interfacename>WebBindingInitializer</interfacename> interface,
          which you then enable by supplying a custom bean configuration for
          an <classname>AnnotationMethodHandlerAdapter</classname>, thus
          overriding the default configuration.</para>

          <para>The following example from the PetClinic application shows a
          configuration using a custom implementation of the
          <interfacename>WebBindingInitializer</interfacename> interface,
          <classname>org.springframework.samples.petclinic.web.ClinicBindingInitializer</classname>,
          which configures PropertyEditors required by several of the
          PetClinic controllers.</para>

          <programlisting language="xml">&lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
    &lt;property name="cacheSeconds" value="0" /&gt;
    &lt;property name="webBindingInitializer"&gt;
        &lt;bean class="org.springframework.samples.petclinic.web.ClinicBindingInitializer" /&gt;
    &lt;/property&gt;
&lt;/bean&gt;
</programlisting>
        </section>
      </section>
    </section>
  </section>

  <section id="mvc-handlermapping">
    <title>Handler mappings</title>

1539
    <para>In previous versions of Spring, users were required to define
1540 1541
    <interfacename>HandlerMapping</interfacename>s in the web application
    context to map incoming web requests to appropriate handlers. With the
1542 1543 1544 1545
    introduction of Spring 2.5, <!--IMPORTANT: Shouldn't that say Spring 3.0, since that's upcoming release? If you do mean 2.5, then first sentence should say in pre--><!--2.5 versions of Spring, not *previous*. Also in first sentence, I changed Spring MVC to Spring because it refers to a version.-->the
    <classname>DispatcherServlet</classname> enables the
    <classname>DefaultAnnotationHandlerMapping</classname>, which looks for
    <interfacename>@RequestMapping</interfacename> annotations on
1546
    <interfacename>@Controllers</interfacename>. Typically, you do not need to
A
Arjen Poutsma 已提交
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
    override this default mapping, unless you need to override the default
    property values. These properties are:</para>

    <variablelist>
      <varlistentry>
        <term><literal>interceptors</literal></term>
        <listitem>
          <para>List of interceptors to use.
          <interfacename>HandlerInterceptor</interfacename>s are discussed in
          <xref linkend="mvc-handlermapping-interceptor" />.</para>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><literal>defaultHandler</literal></term>
        <listitem>
          <para>Default handler to use, when this handler mapping does not
          result in a matching handler.</para>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><literal>order</literal></term>
        <listitem>
          <para>Based on the value of the order property (see the
          <literal>org.springframework.core.Ordered</literal> interface),
          Spring sorts all handler mappings available in the context and
          applies the first matching handler.</para>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><literal>alwaysUseFullPath</literal></term>
        <listitem>
          <para>If <literal>true</literal> , Spring uses the full path within
          the current servlet context to find an appropriate handler. If
          <literal>false</literal> (the default), the path within the current
          servlet mapping is used. For example, if a servlet is mapped using
          <literal>/testing/*</literal> and the
          <literal>alwaysUseFullPath</literal> property is set to true,
          <literal>/testing/viewPage.html</literal> is used, whereas if the
          property is set to false, <literal>/viewPage.html</literal> is
          used.</para>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><literal>urlDecode</literal></term>
        <listitem>
          <para>Defaults to <literal>true</literal>, as of Spring 2.5. <!--OK, or do you mean 3.0?-->
          If you prefer to compare encoded paths, set this flag to
          <literal>false</literal>. However, the
          <interfacename>HttpServletRequest</interfacename> always exposes the
          servlet path in decoded form. Be aware that the servlet path will
          not match when compared with encoded paths.</para>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><literal>lazyInitHandlers</literal></term>
        <listitem>
          <para>Allows lazy initialization of <emphasis>singleton</emphasis>
          handlers (prototype handlers are always lazy-initialized). The
          default value is <literal>false</literal>.</para>
        </listitem>
      </varlistentry>
    </variablelist>

    <note>
      <para>The <literal>alwaysUseFullPath</literal>,
      <literal>urlDecode</literal>, and <literal>lazyInitHandlers</literal>
      properties are only available to subclasses of
      <interfacename>org.springframework.web.servlet.handler.AbstractUrlHandlerMapping</interfacename>.</para>
    </note>
1621

1622
    <para>The following example shows how to override the default mapping and
1623
    add an interceptor:</para>
1624

1625 1626 1627 1628 1629 1630
    <programlisting language="xml">&lt;beans&gt;
  &lt;bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt;
    &lt;property name="interceptors"&gt;
      &lt;bean class="example.MyInterceptor"/&gt;
    &lt;/property&gt;
  &lt;/bean&gt;
1631

1632
&lt;beans&gt;</programlisting>
1633 1634 1635

    <section id="mvc-handlermapping-interceptor">
      <title>Intercepting requests - the
1636
      <interfacename>HandlerInterceptor</interfacename> interface<!--Revise head to delete dash. How should it read? Intercepting requests *through* the HandlerInterceptor Interface? *with*?--></title>
1637

1638 1639 1640
      <para>Spring's handler mapping mechanism includes handler interceptors,
      which are useful when you want to apply specific functionality to
      certain requests, for example, checking for a principal.</para>
1641 1642 1643 1644

      <para>Interceptors located in the handler mapping must implement
      <interfacename>HandlerInterceptor</interfacename> from the
      <literal>org.springframework.web.servlet</literal> package. This
1645 1646 1647 1648 1649 1650 1651
      interface defines three methods: one is called
      <emphasis>before</emphasis> the actual handler is executed; one is
      called <emphasis>after</emphasis> the handler is executed; and one is
      called <emphasis>after the complete request has finished</emphasis>.
      <!--I suggest identifying each method in parentheses after the reference to it, in sentence above. -->These
      three methods should provide enough flexibility to do all kinds of
      preprocessing and postprocessing.</para>
1652 1653 1654 1655

      <para>The <literal>preHandle(..)</literal> method returns a boolean
      value. You can use this method to break or continue the processing of
      the execution chain. When this method returns <literal>true</literal>,
1656
      the handler execution chain will continue; when it returns false, the
1657 1658 1659 1660 1661
      <classname>DispatcherServlet</classname> assumes the interceptor itself
      has taken care of requests (and, for example, rendered an appropriate
      view) and does not continue executing the other interceptors and the
      actual handler in the execution chain.</para>

A
Arjen Poutsma 已提交
1662 1663 1664 1665 1666 1667
      <para>The following example defines a handler mapping which maps all
      requests matching the URL patterns "/*.form" and "/*.view" to a
      particular controller, <literal>editAccountFormController</literal>. An
      interceptor has been added that intercepts these requests and reroutes
      the user to a specific page if the time is not between 9 a.m. and 6
      p.m.</para>
1668

1669
      <programlisting language="xml">&lt;beans&gt;
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
    &lt;bean id="handlerMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
        &lt;property name="interceptors"&gt;
            &lt;list&gt;
                &lt;ref bean="officeHoursInterceptor"/&gt;
            &lt;/list&gt;
        &lt;/property&gt;
        &lt;property name="mappings"&gt;
            &lt;value&gt;
                /*.form=editAccountFormController
                /*.view=editAccountFormController
            &lt;/value&gt;
        &lt;/property&gt;
    &lt;/bean&gt;

    &lt;bean id="officeHoursInterceptor"
          class="samples.TimeBasedAccessInterceptor"&gt;
        &lt;property name="openingTime" value="9"/&gt;
        &lt;property name="closingTime" value="18"/&gt;
    &lt;/bean&gt;
&lt;beans&gt;</programlisting>

1692
      <programlisting language="java">package samples;
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722

public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {

    private int openingTime;
    private int closingTime;

    public void setOpeningTime(int openingTime) {
        this.openingTime = openingTime;
    }

    public void setClosingTime(int closingTime) {
        this.closingTime = closingTime;
    }

    public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler) throws Exception {

        Calendar cal = Calendar.getInstance();
        int hour = cal.get(HOUR_OF_DAY);
        if (openingTime &lt;= hour &lt; closingTime) {
            return true;
        } else {
            response.sendRedirect("http://host.com/outsideOfficeHours.html");
            return false;
        }
    }
}</programlisting>

1723
      <para>Any request handled by this mapping is intercepted by the
1724 1725 1726 1727
      <classname>TimeBasedAccessInterceptor</classname>. If the current time
      is outside office hours, the user is redirected to a static HTML file
      that says, for example, you can only access the website during office
      hours.</para>
1728

1729 1730
      <para>As you can see, the Spring adapter class
      <classname>HandlerInterceptorAdapter</classname> makes it easier to
1731 1732 1733 1734 1735 1736
      extend the <interfacename>HandlerInterceptor</interfacename>
      interface.</para>
    </section>
  </section>

  <section id="mvc-viewresolver">
1737
    <title>Resolving views</title>
1738 1739 1740 1741 1742

    <para>All MVC frameworks for web applications provide a way to address
    views. Spring provides view resolvers, which enable you to render models
    in a browser without tying you to a specific view technology. Out of the
    box, Spring enables you to use JSPs, Velocity templates and XSLT views,
1743 1744
    for example. See <xref linkend="view" /> for a discussion of how to
    integrate and use a number of disparate view technologies.</para>
1745

1746
    <para>The two interfaces that are important to the way Spring handles
1747 1748 1749 1750 1751 1752 1753 1754
    views are <interfacename>ViewResolver</interfacename> and
    <interfacename>View</interfacename>. The
    <interfacename>ViewResolver</interfacename> provides a mapping between
    view names and actual views. The <interfacename>View</interfacename>
    interface addresses the preparation of the request and hands the request
    over to one of the view technologies.</para>

    <section id="mvc-viewresolver-resolver">
1755 1756
      <title>Resolving views with the
      <interfacename>ViewResolver</interfacename> interface</title>
1757

1758
      <para>As discussed in <xref linkend="mvc-controller" />, all handler
A
Arjen Poutsma 已提交
1759 1760 1761 1762 1763 1764 1765
      methods in the Spring Web MVC controllers must resolve to a logical view
      name, either explicitly (e.g., by returning a <literal>String</literal>,
      <literal>View</literal>, or <literal>ModelAndView</literal>) or
      implicitly (i.e., based on conventions). Views in Spring are addressed
      by a logical view name and are resolved by a view resolver. Spring comes
      with quite a few view resolvers. This table lists most of them; a couple
      of examples follow.</para>
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786

      <table id="mvc-view-resolvers-tbl">
        <title>View resolvers</title>

        <tgroup cols="2">
          <colspec colname="c1" colwidth="1*" />

          <colspec colname="c2" colwidth="2*" />

          <thead>
            <row>
              <entry><interfacename>ViewResolver</interfacename></entry>

              <entry>Description</entry>
            </row>
          </thead>

          <tbody>
            <row>
              <entry><classname>AbstractCachingViewResolver</classname></entry>

1787 1788 1789
              <entry>Abstract view resolver that caches views. Often views
              need preparation before they can be used; extending this view
              resolver provides caching.</entry>
1790 1791 1792 1793 1794
            </row>

            <row>
              <entry><classname>XmlViewResolver</classname></entry>

1795
              <entry>Implementation of
1796 1797 1798 1799 1800 1801 1802 1803 1804
              <interfacename>ViewResolver</interfacename> that accepts a
              configuration file written in XML with the same DTD as Spring's
              XML bean factories. The default configuration file is
              <literal>/WEB-INF/views.xml</literal>.</entry>
            </row>

            <row>
              <entry><classname>ResourceBundleViewResolver</classname></entry>

1805
              <entry>Implementation of
1806 1807
              <interfacename>ViewResolver</interfacename> that uses bean
              definitions in a <classname>ResourceBundle</classname>,
1808 1809
              specified by the bundle base name. Typically you define the
              bundle in a properties file, located in the classpath. <!--Correct to say you define? Seems so, because default implies you can change it.-->The
1810 1811 1812 1813 1814 1815 1816
              default file name is
              <literal>views.properties</literal>.</entry>
            </row>

            <row>
              <entry><classname>UrlBasedViewResolver</classname></entry>

1817
              <entry>Simple implementation of the
1818
              <interfacename>ViewResolver</interfacename> interface that
1819
              effects the direct resolution of logical view names to URLs,
1820
              without an explicit mapping definition. This is appropriate if
1821
              your logical names match the names of your view resources in a
1822 1823 1824 1825 1826 1827 1828
              straightforward manner, without the need for arbitrary
              mappings.</entry>
            </row>

            <row>
              <entry><classname>InternalResourceViewResolver</classname></entry>

1829
              <entry>Convenient subclass of
1830
              <classname>UrlBasedViewResolver</classname> that supports
1831
              <classname>InternalResourceView</classname> (in effect, Servlets
A
Arjen Poutsma 已提交
1832 1833 1834
              and JSPs) and subclasses such as <classname>JstlView</classname>
              and <classname>TilesView</classname>. You can specify the view
              class for all views generated by this resolver by using
1835 1836 1837 1838 1839 1840 1841 1842 1843
              <literal>setViewClass(..)</literal>. See the Javadocs for the
              <classname>UrlBasedViewResolver</classname> class for
              details.</entry>
            </row>

            <row>
              <entry><classname>VelocityViewResolver</classname> /
              <classname>FreeMarkerViewResolver</classname></entry>

1844
              <entry>Convenient subclass of
1845
              <classname>UrlBasedViewResolver</classname> that supports
1846 1847 1848
              <classname>VelocityView</classname> (in effect, Velocity
              templates) or <classname>FreeMarkerView</classname>
              ,respectively, and custom subclasses of them.</entry>
1849
            </row>
1850 1851 1852 1853

            <row>
              <entry><classname>ContentNegotiatingViewResolver</classname></entry>

1854 1855
              <entry>Implementation of the
              <interfacename>ViewResolver</interfacename> interface that
1856 1857 1858
              resolves a view based on the request file name or
              <literal>Accept</literal> header. See <xref
              linkend="mvc-multiple-representations" />.</entry>
1859
            </row>
1860 1861 1862 1863
          </tbody>
        </tgroup>
      </table>

1864 1865
      <para>As an example, with JSP as a view technology, you can use the
      <classname>UrlBasedViewResolver</classname>. This view resolver
1866 1867 1868
      translates a view name to a URL and hands the request over to the
      RequestDispatcher to render the view.</para>

1869
      <programlisting language="xml">&lt;bean id="viewResolver"
1870 1871 1872 1873 1874 1875
      class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
    &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt;
    &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
    &lt;property name="suffix" value=".jsp"/&gt;
&lt;/bean&gt;</programlisting>

A
Arjen Poutsma 已提交
1876 1877
      <para>When returning <literal>test</literal> as a logical view name,
      this view resolver forwards the request to the
1878 1879 1880
      <classname>RequestDispatcher</classname> that will send the request to
      <literal>/WEB-INF/jsp/test.jsp</literal>.</para>

1881 1882 1883
      <para>When you combine different view technologies in a web application,
      you can use the
      <classname>ResourceBundleViewResolver</classname>:</para>
1884

1885
      <programlisting language="xml">&lt;bean id="viewResolver"
1886 1887 1888 1889 1890 1891 1892 1893
      class="org.springframework.web.servlet.view.ResourceBundleViewResolver"&gt;
    &lt;property name="basename" value="views"/&gt;
    &lt;property name="defaultParentView" value="parentView"/&gt;
&lt;/bean&gt;</programlisting>

      <para>The <classname>ResourceBundleViewResolver</classname> inspects the
      <classname>ResourceBundle</classname> identified by the basename, and
      for each view it is supposed to resolve, it uses the value of the
1894
      property <literal>[viewname].(class)</literal> as the view class and the
A
Arjen Poutsma 已提交
1895 1896 1897 1898 1899
      value of the property <literal>[viewname].url</literal> as the view url.
      Examples can be found in the next chapter which covers view
      technologies. As you can see, you can identify a parent view, from which
      all views in the properties file <quote>extend</quote>. This way you can
      specify a default view class, for example.</para>
1900 1901 1902 1903

      <note>
        <para>Subclasses of <classname>AbstractCachingViewResolver</classname>
        cache view instances that they resolve. Caching improves performance
1904
        of certain view technologies. It's possible to turn off the cache by
1905 1906 1907 1908 1909 1910
        setting the <literal>cache</literal> property to
        <literal>false</literal>. Furthermore, if you must refresh a certain
        view at runtime (for example when a Velocity template is modified),
        you can use the <literal>removeFromCache(String viewName, Locale
        loc)</literal> method.</para>
      </note>
1911 1912 1913 1914 1915
    </section>

    <section id="mvc-viewresolver-chaining">
      <title>Chaining ViewResolvers</title>

1916 1917 1918 1919
      <para>Spring supports multiple view resolvers. Thus you can chain
      resolvers and, for example, override specific views in certain
      circumstances. You chain view resolvers by adding more than one resolver
      to your application context and, if necessary, by setting the
1920
      <literal>order</literal> property to specify ordering. Remember, the
1921 1922
      higher the order property, the later the view resolver is positioned in
      the chain.</para>
1923 1924

      <para>In the following example, the chain of view resolvers consists of
1925
      two resolvers, an <classname>InternalResourceViewResolver</classname>,
1926 1927 1928 1929
      which is always automatically positioned as the last resolver in the
      chain, and an <classname>XmlViewResolver</classname> for specifying
      Excel views. Excel views are not supported by the
      <classname>InternalResourceViewResolver</classname>.<!--Do you need to say anything else about excel not being supported by one of resolvers? What if anything is the result?--></para>
1930

1931
      <programlisting language="xml">&lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
  &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt;
  &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
  &lt;property name="suffix" value=".jsp"/&gt;
&lt;/bean&gt;

&lt;bean id="excelViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"&gt;
  &lt;property name="order" value="1"/&gt;
  &lt;property name="location" value="/WEB-INF/views.xml"/&gt;
&lt;/bean&gt;

<lineannotation>&lt;!-- in <literal>views.xml</literal> --&gt;</lineannotation>

&lt;beans&gt;
  &lt;bean name="report" class="org.springframework.example.ReportExcelView"/&gt;
&lt;/beans&gt;</programlisting>

1948 1949 1950
      <para>If a specific view resolver does not result in a view, Spring
      examines the context for other view resolvers. If additional view
      resolvers exist, Spring continues to inspect them. <!--So what happens after Spring inspects them?-->If
A
Arjen Poutsma 已提交
1951 1952
      they do not exist, Spring throws an
      <classname>Exception</classname>.</para>
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964

      <para>The contract of a view resolver specifies that a view resolver
      <emphasis>can</emphasis> return null to indicate the view could not be
      found. Not all view resolvers do this, however, because in some cases,
      the resolver simply cannot detect whether or not the view exists. For
      example, the <classname>InternalResourceViewResolver</classname> uses
      the <classname>RequestDispatcher</classname> internally, and dispatching
      is the only way to figure out if a JSP exists, but this action can only
      execute once. The same holds for the
      <classname>VelocityViewResolver</classname> and some others. Check the
      Javadoc for the view resolver to see whether it reports non-existing
      views. Thus, putting an
1965
      <classname>InternalResourceViewResolver</classname> in the chain in a
1966 1967 1968 1969
      place other than the last, results in the chain not being fully
      inspected, because the
      <classname>InternalResourceViewResolver</classname> will
      <emphasis>always</emphasis> return a view!<!--I don't understand the logic of this. How can it return a view if no view exists or no view can be found? this paragraph is confusing.--><!--Why would you put InternalResourceViewResolver in place other than last? It's automatically last. --></para>
1970 1971 1972
    </section>

    <section id="mvc-redirecting">
1973
      <title>Redirecting to views<!--Revise to say what you are redirecting to views. OR are you redirecting views? In that case heading should be Redirecting views.--></title>
1974

1975
      <para>As mentioned previously, a controller typically returns a logical
1976
      view name, which a view resolver resolves to a particular view
1977 1978
      technology. For view technologies such as JSPs that are processed
      through the Servlet or JSP engine, this resolution is usually handled
A
Arjen Poutsma 已提交
1979 1980 1981 1982
      through the combination of
      <classname>InternalResourceViewResolver</classname> and
      <classname>InternalResourceView</classname>, which issues an internal
      forward or include via the Servlet API's
1983 1984
      <literal>RequestDispatcher.forward(..)</literal> method or
      <literal>RequestDispatcher.include()</literal> method. For other view
A
Arjen Poutsma 已提交
1985 1986
      technologies, such as Velocity, XSLT, and so on, the view itself writes
      the content directly to the response stream.</para>
1987 1988

      <para>It is sometimes desirable to issue an HTTP redirect back to the
A
Arjen Poutsma 已提交
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
      client, before the view is rendered. This is desirable, for example,
      when one controller has been called with <literal>POST</literal>ed data,
      and the response is actually a delegation to another controller (for
      example on a successful form submission). In this case, a normal
      internal forward will mean that the other controller will also see the
      same <literal>POST</literal> data, which is potentially problematic if
      it can confuse it with other expected data. Another reason to perform a
      redirect before displaying the result is to eliminate the possibility of
      the user submitting the form data multiple times. In this scenario, the
      browser will first send an initial <literal>POST</literal>; it will then
      receive a response to redirect to a different URL; and finally the
      browser will perform a subsequent <literal>GET</literal> for the URL
      named in the redirect response. Thus, from the perspective of the
2002
      browser, the current page does not reflect the result of a
A
Arjen Poutsma 已提交
2003 2004 2005 2006 2007
      <literal>POST</literal> but rather of a <literal>GET</literal>. The end
      effect is that there is no way the user can accidentally
      re-<literal>POST</literal> the same data by performing a refresh. The
      refresh forces a <literal>GET</literal> of the result page, not a resend
      of the initial <literal>POST</literal> data.</para>
2008 2009 2010 2011 2012 2013 2014

      <section id="mvc-redirecting-redirect-view">
        <title><classname>RedirectView</classname></title>

        <para>One way to force a redirect as the result of a controller
        response is for the controller to create and return an instance of
        Spring's <classname>RedirectView</classname>. In this case,
2015 2016 2017 2018 2019 2020
        <classname>DispatcherServlet</classname> does not use the normal view
        resolution mechanism. Rather because it has been given the (redirect)
        view already, the <classname>DispatcherServlet</classname> simply
        instructs the view to do its work.</para>

        <para>The <classname>RedirectView</classname> issues an
2021 2022
        <literal>HttpServletResponse.sendRedirect()</literal> call that
        returns to the client browser as an HTTP redirect. <!--Does preceding happen after what happens in first paragraph? Clarify sequence of events.-->All
A
Arjen Poutsma 已提交
2023 2024 2025 2026
        model attributes are exposed as HTTP query parameters. This means that
        the model must contain only objects (generally Strings or objects
        converted to a String representation), which can be readily converted
        to a textual HTTP query parameter.</para>
2027

2028 2029 2030 2031 2032 2033
        <para>If you use <classname>RedirectView</classname> and the view is
        created by the controller itself, it is recommended that you configure
        the redirect URL to be injected into the controller so that it is not
        baked into the controller but configured in the context along with the
        view names. <!--I revised sentence because it sounds like something you need to do. Also reworded next heading to say what it's about. If not correct,--><!--reword.-->The
        next section discusses this process.</para>
2034 2035 2036 2037 2038 2039
      </section>

      <section id="mvc-redirecting-redirect-prefix">
        <title>The <literal>redirect:</literal> prefix</title>

        <para>While the use of <classname>RedirectView</classname> works fine,
2040
        if the controller itself creates the
A
Arjen Poutsma 已提交
2041 2042 2043 2044 2045 2046
        <classname>RedirectView</classname>, there is no avoiding the fact
        that the controller is aware that a redirection is happening. This is
        really suboptimal and couples things too tightly. The controller
        should not really care about how the response gets handled. In general
        it should operate only in terms of view names that have been injected
        into it.</para>
2047

2048 2049
        <para>The special <literal>redirect:</literal> prefix allows you to
        accomplish this. If a view name is returned that has the prefix
A
Arjen Poutsma 已提交
2050 2051 2052 2053
        <literal>redirect:</literal>, the
        <classname>UrlBasedViewResolver</classname> (and all subclasses) will
        recognize this as a special indication that a redirect is needed. The
        rest of the view name will be treated as the redirect URL.</para>
2054 2055 2056

        <para>The net effect is the same as if the controller had returned a
        <classname>RedirectView</classname>, but now the controller itself can
2057 2058 2059
        simply operate in terms of logical view names. A logical view name
        such as <literal>redirect:/my/response/controller.html</literal> will
        redirect relative to the current servlet context, while a name such as
2060
        <literal>redirect:http://myhost.com/some/arbitrary/path.html</literal>
2061
        will redirect to an absolute URL. The important thing is that, as long
2062 2063 2064 2065 2066 2067
        as this redirect view name is injected into the controller like any
        other logical view name, the controller is not even aware that
        redirection is happening.</para>
      </section>

      <section id="mvc-redirecting-forward-prefix">
2068
        <title>The <literal>forward:</literal> prefix<!--Can you revise this heading to say what you're using the forward prefix to accomplish?--></title>
2069 2070

        <para>It is also possible to use a special <literal>forward:</literal>
2071
        prefix for view names that are ultimately resolved by
2072 2073
        <classname>UrlBasedViewResolver</classname> and subclasses. This
        creates an <classname>InternalResourceView</classname> (which
2074 2075
        ultimately does a <literal>RequestDispatcher.forward()</literal>)
        around the rest of the view name, which is considered a URL.
2076
        Therefore, this prefix is not useful with
2077
        <classname>InternalResourceViewResolver</classname> and
2078 2079 2080 2081 2082
        <classname>InternalResourceView</classname> (for JSPs for example).
        But the prefix can be helpful when you are primarily using another
        view technology, but still want to force a forward of a resource to be
        handled by the Servlet/JSP engine. (Note that you may also chain
        multiple view resolvers, instead.)<!--I think the preceding sentences were a bit garbled. I tried to reword a bit. And is this paragraph logical?--></para>
2083 2084

        <para>As with the <literal>redirect:</literal> prefix, if the view
A
Arjen Poutsma 已提交
2085 2086 2087
        name with the <literal>forward:</literal> prefix is injected into the
        controller, the controller does not detect that anything special is
        happening in terms of handling the response.<!--Can you reword to clarify the point? The controller does not detect what?--></para>
2088 2089
      </section>
    </section>
2090

2091 2092 2093 2094
    <section id="mvc-multiple-representations">
      <title><classname>ContentNegotiatingViewResolver</classname></title>

      <para>The <classname>ContentNegotiatingViewResolver</classname> does not
2095
      resolve views itself but rather delegates to other view resolvers,
2096
      selecting the view that resembles the representation requested by the
2097 2098
      client. Two strategies exist for a client to request a representation
      from the server:</para>
2099

2100 2101
      <itemizedlist>
        <listitem>
A
Arjen Poutsma 已提交
2102 2103 2104
          <para>Use a distinct URI for each resource, typically by using a
          different file extension in the URI. For example, the URI<literal>
          http://www.example.com/users/fred.pdf</literal> requests a PDF
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
          representation of the user fred, and
          <literal>http://www.example.com/users/fred.xml</literal> requests an
          XML representation.</para>
        </listitem>
      </itemizedlist>

      <itemizedlist>
        <listitem>
          <para>Use the same URI for the client to locate the resource, but
          set the <literal>Accept</literal> HTTP request header to list the
          <ulink url="http://en.wikipedia.org/wiki/Internet_media_type">media
          types</ulink> that it understands. For example, an HTTP request for
A
Arjen Poutsma 已提交
2117 2118 2119 2120 2121 2122 2123 2124
          <literal>http://www.example.com/users/fred</literal> with an
          <literal>Accept</literal> header set to <literal>application/pdf
          </literal>requests a PDF representation of the user fred, while
          <literal>http://www.example.com/users/fred</literal> with an
          <literal>Accept</literal> header set to <literal>text/xml</literal>
          requests an XML representation. This strategy is known as <ulink
          url="http://en.wikipedia.org/wiki/Content_negotiation">content
          negotiation</ulink>.</para>
2125 2126
        </listitem>
      </itemizedlist>
2127 2128

      <note>
A
Arjen Poutsma 已提交
2129 2130 2131
        <para>One issue with the <literal>Accept</literal> header is that it
        is impossible to set it in a web browser within HTML. For example, in
        Firefox, it is fixed to:<!--So how would you set the Accept header as in second bullet, if you can't do it in html? Indicate?--></para>
2132

2133
        <programlisting>Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8</programlisting>
2134

2135
        <para>For this reason it is common to see the use of a distinct URI
A
Arjen Poutsma 已提交
2136 2137
        for each representation when developing browser based web
        applications.</para>
2138 2139
      </note>

2140
      <para>To support multiple representations of a resource, Spring provides
2141 2142 2143
      the <classname>ContentNegotiatingViewResolver</classname> to resolve a
      view based on the file extension or <literal>Accept</literal> header of
      the HTTP request. <classname>ContentNegotiatingViewResolver</classname>
2144
      does not perform the view resolution itself but instead delegates to a
2145 2146
      list of view resolvers that you specify through the bean property
      <literal>ViewResolvers</literal>.<!--A human has to specify this list of resolvers, right? See example below.--></para>
2147 2148 2149

      <para>The <classname>ContentNegotiatingViewResolver</classname> selects
      an appropriate <classname>View</classname> to handle the request by
2150
      comparing the request media type(s) with the media type (also known as
2151 2152 2153 2154
      <literal>Content-Type</literal>) supported by the
      <classname>View</classname> associated with each of its
      <classname>ViewResolvers</classname>. The first
      <classname>View</classname> in the list that has a compatible
2155
      <literal>Content-Type</literal> returns the representation to the
A
Arjen Poutsma 已提交
2156 2157 2158 2159 2160 2161 2162 2163
      client. If a compatible view cannot be supplied by the
      <classname>ViewResolver</classname> chain, then the list of views
      specified through the <literal>DefaultViews</literal> property will be
      consulted. This latter option is appropriate for singleton
      <classname>Views</classname> that can render an appropriate
      representation of the current resource regardless of the logical view
      name. The <literal>Accept</literal> header may include wildcards, for
      example text/*, in which case a <classname>View</classname> whose
2164 2165
      Context-Type was text/xml is a compatible match.</para>

A
Arjen Poutsma 已提交
2166 2167 2168 2169 2170
      <para>To support the resolution of a view based on a file extension, use
      the <classname>ContentNegotiatingViewResolver </classname>bean property
      <literal>mediaTypes</literal> to specify a mapping of file extensions to
      media types. For more information on the algorithm used to determine the
      request media type, refer to the API documentation for
2171
      <classname>ContentNegotiatingViewResolver</classname>.</para>
2172 2173

      <para>Here is an example configuration of a
2174
      <classname>ContentNegotiatingViewResolver:</classname></para>
2175 2176 2177 2178 2179 2180

      <programlisting language="xml">&lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt;
  &lt;property name="mediaTypes"&gt;
    &lt;map&gt;
      &lt;entry key="atom" value="application/atom+xml"/&gt;
      &lt;entry key="html" value="text/html"/&gt;
2181
      &lt;entry key="json" value="application/json"/&gt;
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
    &lt;/map&gt;
  &lt;/property&gt;
  &lt;property name="viewResolvers"&gt;
    &lt;list&gt;
      &lt;bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/&gt;
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
        &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
        &lt;property name="suffix" value=".jsp"/&gt;
      &lt;/bean&gt;
    &lt;/list&gt;
  &lt;/property&gt;
A
Arjen Poutsma 已提交
2193 2194 2195 2196
  &lt;property name="defaultViews"&gt;
    &lt;list&gt;
      &lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /&gt;
    &lt;/list&gt;
2197
  &lt;/property&gt;
2198 2199 2200 2201 2202 2203
&lt;/bean&gt;


&lt;bean id="content" class="com.springsource.samples.rest.SampleContentAtomView"/&gt;</programlisting>

      <para>The <classname>InternalResourceViewResolver</classname> handles
2204
      the translation of view names and JSP pages, while the
2205 2206
      <classname>BeanNameViewResolver</classname> returns a view based on the
      name of a bean. (See "<link
2207 2208 2209 2210 2211 2212 2213 2214
      linkend="mvc-viewresolver-resolver">Resolving views with the
      ViewResolver interface</link>" for more details on how Spring looks up
      and instantiates a view.) In this example, the
      <literal>content</literal> bean is a class that inherits from
      <classname>AbstractAtomFeedView</classname>, which returns an Atom RSS
      feed. For more information on creating an Atom Feed representation, see
      the section Atom Views.<!--Need a correct link or x-ref re the preceding sentence.I couldn't find an "Atom Views" section.--></para>

A
Arjen Poutsma 已提交
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232
      <para>In the above configuration, if a request is made with an
      <literal>.html</literal> extension, the view resolver looks for a view
      that matches the <literal>text/html</literal> media type. The
      <classname>InternalResourceViewResolver</classname> provides the
      matching view for <literal>text/html</literal>. If the request is made
      with the file extension <literal>.atom</literal>, the view resolver
      looks for a view that matches the
      <literal>application/atom+xml</literal> media type. This view is
      provided by the <classname>BeanNameViewResolver</classname> that maps to
      the <classname>SampleContentAtomView</classname> if the view name
      returned is <classname>content</classname>. If the request is made with
      the file extension <literal>.json</literal>, the
      <classname>MappingJacksonJsonView</classname> instance from the
      <literal>DefaultViews</literal> list will be selected regardless of the
      view name. Alternatively, client requests can be made without a file
      extension but with the <literal>Accept</literal> header set to the
      preferred media-type, and the same resolution of request to views would
      occur.<!--Can you reword preceding sentence? I don't follow it.--></para>
2233 2234 2235

      <note>
        <para>If <classname>ContentNegotiatingViewResolver</classname>'s list
A
Arjen Poutsma 已提交
2236 2237
        of ViewResolvers is not configured explicitly, it automatically uses
        any ViewResolvers defined in the application context.</para>
2238 2239 2240 2241 2242 2243
      </note>

      <para>The corresponding controller code that returns an Atom RSS feed
      for a URI of the form <literal>http://localhost/content.atom</literal>
      or <literal>http://localhost/content</literal> with an
      <literal>Accept</literal> header of application/atom+xml is shown
2244
      below.</para>
2245 2246

      <programlisting language="java">@Controller
2247
public class ContentController {
2248

2249
    private List&lt;SampleContent&gt; contentList = new ArrayList&lt;SampleContent&gt;();
2250

2251 2252 2253 2254 2255 2256 2257
    @RequestMapping(value="/content", method=RequestMethod.GET)
    public ModelAndView getContent() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("content");
        mav.addObject("sampleContentList", contentList);
        return mav;
    }
2258

2259
}</programlisting>
2260
    </section>
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
  </section>

  <section id="mvc-localeresolver">
    <title>Using locales</title>

    <para>Most parts of Spring's architecture support internationalization,
    just as the Spring web MVC framework does.
    <classname>DispatcherServlet</classname> enables you to automatically
    resolve messages using the client's locale. This is done with
    <interfacename>LocaleResolver</interfacename> objects.</para>

    <para>When a request comes in, the
2273
    <classname>DispatcherServlet</classname> looks for a locale resolver, and
2274 2275 2276 2277
    if it finds one it tries to use it to set the locale. Using the
    <literal>RequestContext.getLocale()</literal> method, you can always
    retrieve the locale that was resolved by the locale resolver.</para>

2278
    <para>In addition to automatic locale resolution, you can also attach an
2279 2280
    interceptor to the handler mapping (see <xref
    linkend="mvc-handlermapping-interceptor" /> for more information on
2281 2282
    handler mapping interceptors) to change the locale under specific
    circumstances, for example, based on a parameter in the request.</para>
2283

2284 2285
    <para>Locale resolvers and interceptors are defined in the
    <literal>org.springframework.web.servlet.i18n</literal> package and are
2286 2287 2288 2289 2290 2291 2292 2293
    configured in your application context in the normal way. Here is a
    selection of the locale resolvers included in Spring.</para>

    <section id="mvc-localeresolver-acceptheader">
      <title><classname>AcceptHeaderLocaleResolver</classname></title>

      <para>This locale resolver inspects the
      <literal>accept-language</literal> header in the request that was sent
A
Arjen Poutsma 已提交
2294 2295
      by the client (e.g., a web browser). Usually this header field contains
      the locale of the client's operating system.</para>
2296 2297 2298 2299 2300 2301
    </section>

    <section id="mvc-localeresolver-cookie">
      <title><classname>CookieLocaleResolver</classname></title>

      <para>This locale resolver inspects a <classname>Cookie</classname> that
2302 2303 2304
      might exist on the client to see if a locale is specified. If so, it
      uses the specified locale. Using the properties of this locale resolver,
      you can specify the name of the cookie as well as the maximum age. Find
A
Arjen Poutsma 已提交
2305 2306
      below an example of defining a
      <classname>CookieLocaleResolver</classname>.</para>
2307

2308
      <programlisting language="xml">&lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"&gt;
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351

    &lt;property name="cookieName" value="clientlanguage"/&gt;
    
    <lineannotation>&lt;!-- in seconds. If set to <literal>-1</literal>, the cookie is not persisted (deleted when browser shuts down) --&gt;</lineannotation>
    &lt;property name="cookieMaxAge" value="100000"&gt;

&lt;/bean&gt;</programlisting>

      <table id="mvc-cookie-locale-resolver-props-tbl">
        <title><classname>CookieLocaleResolver</classname> properties</title>

        <tgroup cols="3">
          <colspec colname="c1" colwidth="1*" />

          <colspec colname="c2" colwidth="1*" />

          <colspec colname="c3" colwidth="3*" />

          <thead>
            <row>
              <entry>Property</entry>

              <entry>Default</entry>

              <entry>Description</entry>
            </row>
          </thead>

          <tbody>
            <row>
              <entry>cookieName</entry>

              <entry>classname + LOCALE</entry>

              <entry>The name of the cookie</entry>
            </row>

            <row>
              <entry>cookieMaxAge</entry>

              <entry>Integer.MAX_INT</entry>

              <entry>The maximum time a cookie will stay persistent on the
2352
              client. If -1 is specified, the cookie will not be persisted; it
2353 2354 2355 2356 2357 2358 2359 2360 2361
              will only be available until the client shuts down his or her
              browser.</entry>
            </row>

            <row>
              <entry>cookiePath</entry>

              <entry>/</entry>

2362
              <entry>Limits the visibility of the cookie to a certain part of
A
Arjen Poutsma 已提交
2363 2364
              your site. When cookiePath is specified, the cookie will only be
              visible to that path and the paths below it.</entry>
2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
            </row>
          </tbody>
        </tgroup>
      </table>
    </section>

    <section id="mvc-localeresolver-session">
      <title><classname>SessionLocaleResolver</classname></title>

      <para>The <classname>SessionLocaleResolver</classname> allows you to
      retrieve locales from the session that might be associated with the
2376
      user's request.<!--Aren't you missing some information and example? This section has only one sentence.--></para>
2377 2378 2379 2380 2381
    </section>

    <section id="mvc-localeresolver-interceptor">
      <title><classname>LocaleChangeInterceptor</classname></title>

2382
      <para>You can enable changing of locales by adding the
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
      <classname>LocaleChangeInterceptor</classname> to one of the handler
      mappings (see <xref linkend="mvc-handlermapping" />). It will detect a
      parameter in the request and change the locale. It calls
      <literal>setLocale()</literal> on the
      <interfacename>LocaleResolver</interfacename> that also exists in the
      context. The following example shows that calls to all
      <literal>*.view</literal> resources containing a parameter named
      <literal>siteLanguage</literal> will now change the locale. So, for
      example, a request for the following URL,
      <literal>http://www.sf.net/home.view?siteLanguage=nl</literal> will
      change the site language to Dutch.</para>
2394

2395
      <programlisting language="xml">&lt;bean id="localeChangeInterceptor"
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
    &lt;property name="paramName" value="siteLanguage"/&gt;
&lt;/bean&gt;

&lt;bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/&gt;

&lt;bean id="urlMapping"
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
    &lt;property name="interceptors"&gt;
        &lt;list&gt;
            &lt;ref bean="localeChangeInterceptor"/&gt;
        &lt;/list&gt;
    &lt;/property&gt;
    &lt;property name="mappings"&gt;
        &lt;value&gt;/**/*.view=someController&lt;/value&gt;
    &lt;/property&gt;
&lt;/bean&gt;</programlisting>
    </section>
  </section>

  <section id="mvc-themeresolver">
    <title>Using themes</title>

    <section id="mvc-themeresolver-introduction">
2421
      <title>Overview of themes</title>
2422

A
Arjen Poutsma 已提交
2423 2424 2425 2426
      <para>You can apply Spring Web MVC framework themes to set the overall
      look-and-feel of your application, thereby enhancing user experience. A
      theme is a collection of static resources, typically style sheets and
      images, that affect the visual style of the application.</para>
2427 2428 2429 2430 2431
    </section>

    <section id="mvc-themeresolver-defining">
      <title>Defining themes</title>

A
Arjen Poutsma 已提交
2432 2433 2434 2435 2436 2437 2438
      <para>To use themes in your web application, you must set up an
      implementation of the
      <interfacename>org.springframework.ui.context.ThemeSource</interfacename>
      interface. The <interfacename>WebApplicationContext</interfacename>
      interface extends <interfacename>ThemeSource</interfacename> but
      delegates its responsibilities to a dedicated implementation. By default
      the delegate will be an
2439
      <classname>org.springframework.ui.context.support.ResourceBundleThemeSource</classname>
2440 2441 2442
      implementation that loads properties files from the root of the
      classpath. To use a custom <interfacename>ThemeSource</interfacename>
      implementation or to configure the base name prefix of the
2443
      <classname>ResourceBundleThemeSource</classname>, you can register a
2444 2445
      bean in the application context with the reserved name
      <classname>themeSource</classname>. The web application context
2446
      automatically detects a bean with that name and uses it.</para>
2447 2448

      <para>When using the <classname>ResourceBundleThemeSource</classname>, a
2449 2450 2451
      theme is defined in a simple properties file. <!--Revise preceding sentence to clarify: To use ResourceBundleThemeSource, you define a theme in a properties file? OR do you mean a theme--><!--is already defined in a simple properties file for use with ResourceBundleThemeSource?-->The
      properties file lists the resources that make up the theme. Here is an
      example:<!--Is this an example of what a human enters? If not, why is it referred to as an example, if this is exact code already provided?--></para>
2452 2453 2454 2455

      <programlisting>styleSheet=/themes/cool/style.css
background=/themes/cool/img/coolBg.jpg</programlisting>

2456 2457 2458 2459 2460
      <para>The keys of the properties are the names that refer to the themed
      elements from view code. For a JSP, you typically do this using the
      <literal>spring:theme</literal> custom tag, which is very similar to the
      <literal>spring:message</literal> tag. The following JSP fragment uses
      the theme defined in the previous example to customize the look and
2461 2462
      feel:</para>

2463
      <programlisting language="xml">&lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%&gt;
2464 2465 2466 2467
&lt;html&gt;
   &lt;head&gt;
      &lt;link rel="stylesheet" href="&lt;spring:theme code="styleSheet"/&gt;" type="text/css"/&gt;
   &lt;/head&gt;
2468
   &lt;body style="background=&lt;spring:theme code="background"/&gt;"&gt;
2469 2470 2471 2472 2473
      ...
   &lt;/body&gt;
&lt;/html&gt;</programlisting>

      <para>By default, the <classname>ResourceBundleThemeSource</classname>
2474
      uses an empty base name prefix. As a result, the properties files are
2475
      loaded from the root of the classpath. Thus you would put the
2476
      <literal>cool.properties</literal> theme definition in a directory at
2477 2478 2479 2480 2481
      the root of the classpath, for example, in
      <literal>/WEB-INF/classes</literal>. The
      <classname>ResourceBundleThemeSource</classname> uses the standard Java
      resource bundle loading mechanism, allowing for full
      internationalization of themes. For example, we could have a
2482
      <literal>/WEB-INF/classes/cool_nl.properties</literal> that references a
2483
      special background image with Dutch text on it.</para>
2484 2485 2486 2487 2488
    </section>

    <section id="mvc-themeresolver-resolving">
      <title>Theme resolvers</title>

2489 2490 2491 2492 2493 2494 2495 2496
      <para>After you define themes, as in the preceding section, you decide
      which theme to use. The <classname>DispatcherServlet</classname> will
      look for a bean named <classname>themeResolver</classname> to find out
      which <interfacename>ThemeResolver</interfacename> implementation to
      use. A theme resolver works in much the same way as a
      <interfacename>LocaleResolver</interfacename>. It detects the theme to
      use for a particular request and can also alter the request's theme. The
      following theme resolvers are provided by Spring:</para>
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518

      <table id="mvc-theme-resolver-impls-tbl">
        <title><interfacename>ThemeResolver</interfacename>
        implementations</title>

        <tgroup cols="2">
          <colspec colname="c1" colwidth="1*" />

          <colspec colname="c3" colwidth="3*" />

          <thead>
            <row>
              <entry>Class</entry>

              <entry>Description</entry>
            </row>
          </thead>

          <tbody>
            <row>
              <entry><classname>FixedThemeResolver</classname></entry>

2519 2520
              <entry>Selects a fixed theme, set using the
              <classname>defaultThemeName</classname> property.</entry>
2521 2522 2523 2524 2525
            </row>

            <row>
              <entry><classname>SessionThemeResolver</classname></entry>

2526
              <entry>The theme is maintained in the user's HTTP session. It
2527 2528 2529 2530 2531 2532 2533
              only needs to be set once for each session, but is not persisted
              between sessions.</entry>
            </row>

            <row>
              <entry><classname>CookieThemeResolver</classname></entry>

A
Arjen Poutsma 已提交
2534 2535
              <entry>The selected theme is stored in a cookie on the
              client.</entry>
2536 2537 2538 2539 2540 2541
            </row>
          </tbody>
        </tgroup>
      </table>

      <para>Spring also provides a
A
Arjen Poutsma 已提交
2542 2543
      <classname>ThemeChangeInterceptor</classname> that allows theme changes
      on every request with a simple request parameter.<!--Do you need more info or an example re preceding sentence?--></para>
2544 2545 2546 2547 2548 2549 2550 2551 2552
    </section>
  </section>

  <section id="mvc-multipart">
    <title>Spring's multipart (fileupload) support</title>

    <section id="mvc-multipart-introduction">
      <title>Introduction</title>

2553 2554 2555
      <para>Spring's built-in multipart support handles file uploads in web
      applications. You enable this multipart support with pluggable
      <interfacename>MultipartResolver</interfacename> objects, defined in the
A
Arjen Poutsma 已提交
2556 2557 2558 2559
      <literal>org.springframework.web.multipart</literal> package. Spring
      provides a <interfacename>MultipartResolver</interfacename> for use with
      <ulink url="http://jakarta.apache.org/commons/fileupload">
      <emphasis>Commons FileUpload</emphasis></ulink>).</para>
2560

2561 2562 2563 2564 2565 2566 2567 2568 2569
      <para>By default, Spring does no multipart handling, because some
      developers want to handle multiparts themselves. You enable Spring
      multipart handling by adding a multipart resolver to the web
      application's context. Each request is inspected to see if it contains a
      multipart. If no multipart is found, the request continues as expected.
      If a multipart is found in the request, the
      <classname>MultipartResolver</classname> that has been declared in your
      context is used. After that, the multipart attribute in your request is
      treated like any other attribute.</para>
2570 2571 2572 2573 2574 2575 2576 2577 2578
    </section>

    <section id="mvc-multipart-resolver">
      <title>Using the
      <interfacename>MultipartResolver</interfacename></title>

      <para>The following example shows how to use the
      <classname>CommonsMultipartResolver</classname>:</para>

2579
      <programlisting language="xml">&lt;bean id="multipartResolver"
2580 2581 2582 2583 2584 2585 2586 2587 2588
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;

    <lineannotation>&lt;!-- one of the properties available; the maximum file size in bytes --&gt;</lineannotation>
    &lt;property name="maxUploadSize" value="100000"/&gt;
&lt;/bean&gt;</programlisting>

      <para>Of course you also need to put the appropriate jars in your
      classpath for the multipart resolver to work. In the case of the
      <classname>CommonsMultipartResolver</classname>, you need to use
2589
      <literal>commons-fileupload.jar</literal>.</para>
2590

2591 2592 2593 2594 2595
      <para>When the Spring <classname>DispatcherServlet</classname> detects a
      multi-part request, it activates the resolver that has been declared in
      your context and hands over the request. The resolver then wraps the
      current <classname>HttpServletRequest</classname> into a
      <classname>MultipartHttpServletRequest</classname> that supports
2596
      multipart file uploads. Using the
A
Arjen Poutsma 已提交
2597
      <classname>MultipartHttpServletRequest</classname>, you can get
2598 2599 2600 2601 2602 2603 2604
      information about the multiparts contained by this request and actually
      get access to the multipart files themselves in your controllers.</para>
    </section>

    <section id="mvc-multipart-forms">
      <title>Handling a file upload in a form</title>

2605
      <para>After the <classname>MultipartResolver</classname> completes its
A
Arjen Poutsma 已提交
2606 2607 2608 2609
      job, the request is processed like any other. First, create a form with
      a file input that will allow the user to upload a form. The encoding
      attribute (<literal>enctype="multipart/form-data"</literal>) lets the
      browser know how to encode the form as multipart request:</para>
2610

2611
      <programlisting language="xml">&lt;html&gt;
2612 2613 2614 2615 2616
    &lt;head&gt;
        &lt;title&gt;Upload a file please&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1&gt;Please upload a file&lt;/h1&gt;
A
Arjen Poutsma 已提交
2617 2618
        &lt;form method="post" action="/form" enctype="multipart/form-data"&gt;
            &lt;input type="text" name="name"/&gt;
2619 2620 2621 2622 2623 2624
            &lt;input type="file" name="file"/&gt;
            &lt;input type="submit"/&gt;
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;</programlisting>

A
Arjen Poutsma 已提交
2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
      <para>The next step is to create a controller that handles the file
      upload.This controller is very similar to a <link
      linkend="mvc-ann-controller">normal annotated
      <interfacename>@Controllers</interfacename></link>, except that we use
      <classname>MultipartHttpServletRequest</classname> or
      <filename>MultipartFile</filename> in the method parameters:
      <programlisting language="java">@Controller
public class FileUpoadController {

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String handleFormUpload(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            <lineannotation>// store the bytes somewhere</lineannotation>
           return "redirect:uploadSuccess";
       } else {
           return "redirect:uploadFailure";
       }
2645 2646
    }

A
Arjen Poutsma 已提交
2647
}</programlisting></para>
2648

A
Arjen Poutsma 已提交
2649 2650 2651 2652 2653
      <para>Note how the <interfacename>@RequestParam</interfacename> method
      parameters map to the input elements declared in the form. In this
      example, nothing is done with the <literal>byte[]</literal>, but in
      practice you can save it in a database, store it on the file system, and
      so on.</para>
2654

A
Arjen Poutsma 已提交
2655 2656
      <para>Finally, you will have to declare the controller and the resolver
      in the application context:</para>
2657

A
Arjen Poutsma 已提交
2658 2659 2660 2661 2662
      <programlisting language="xml">&lt;beans&gt;
    &lt;bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/&gt;
    <lineannotation>&lt;!-- Declare explicitly, or use &lt;context:annotation-config/&gt; --&gt;</lineannotation>
    &lt;bean id="fileUploadController" class="examples.FileUploadController"/&gt;
2663

A
Arjen Poutsma 已提交
2664
&lt;/beans&gt;</programlisting>
2665 2666 2667 2668 2669 2670
    </section>
  </section>

  <section id="mvc-exceptionhandlers">
    <title>Handling exceptions</title>

2671 2672 2673 2674 2675 2676 2677
    <section>
      <title
      id="mvc-HandlerExceptionResolver"><interfacename>HandlerExceptionResolver<!--I thought HandlerExceptionResolver needed its own section.--></interfacename></title>

      <para>Spring <literal>HandlerExceptionResolvers</literal> ease the pain
      of unexpected exceptions that occur while your request is handled by a
      controller that matched the request.
A
Arjen Poutsma 已提交
2678 2679 2680
      <literal>HandlerExceptionResolvers</literal> somewhat resemble the
      exception mappings you can define in the web application descriptor
      <literal>web.xml</literal>. However, they provide a more flexible way to
2681
      handle exceptions. They provide information about which handler was
A
Arjen Poutsma 已提交
2682
      executing when the exception was thrown. Furthermore, a programmatic way
2683 2684 2685 2686
      of handling exception gives you more options for responding
      appropriately before the request is forwarded to another URL (the same
      end result as when you use the servlet specific exception
      mappings).</para>
2687

A
Arjen Poutsma 已提交
2688 2689
      <para>Besides implementing the
      <interfacename>HandlerExceptionResolver</interfacename> interface, which
2690 2691 2692
      is only a matter of implementing the
      <literal>resolveException(Exception, Handler)</literal> method and
      returning a <classname>ModelAndView</classname>, you may also use the
A
Arjen Poutsma 已提交
2693 2694 2695
      <classname>SimpleMappingExceptionResolver</classname>. This resolver
      enables you to take the class name of any exception that might be thrown
      and map it to a view name. This is functionally equivalent to the
2696 2697
      exception mapping feature from the Servlet API, but it is also possible
      to implement more finely grained mappings of exceptions from different
A
Arjen Poutsma 已提交
2698
      handlers.</para>
2699
    </section>
2700 2701

    <section id="mvc-ann-exceptionhandler">
2702
      <title><interfacename>@ExceptionHandler<!--Changed this from @ExceptionResolver because text and example say @ExceptionHandler.--></interfacename></title>
2703

2704 2705 2706 2707 2708 2709 2710
      <para>An alternative to the
      <interfacename>HandlerExceptionResolver</interfacename> interface is the
      <interfacename>@ExceptionHandler</interfacename> annotation. You use the
      <classname>@ExceptionHandler</classname> method annotation within a
      controller to specify which method is invoked when an exception of a
      specific type is thrown during the execution of controller methods. For
      example:</para>
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740

      <programlisting language="java">@Controller
public class SimpleController {

  // other controller method omitted

  @ExceptionHandler(IOException.class)
  public String handleIOException(IOException ex, HttpServletRequest request) {
    return ClassUtils.getShortName(ex.getClass());
  }
}</programlisting>

      <para>will invoke the 'handlerIOException' method when a
      <classname>java.io.IOException</classname> is thrown.</para>

      <para>The <classname>@ExceptionHandler</classname> value can be set to
      an array of Exception types. If an exception is thrown matches one of
      the types in the list, then the method annotated with the matching
      <classname>@ExceptionHandler</classname> will be invoked. If the
      annotation value is not set then the exception types listed as method
      arguments are used.</para>

      <para>Much like standard controller methods annotated with a
      <classname>@RequestMapping</classname> annotation, the method arguments
      and return values of <classname>@ExceptionHandler</classname> methods
      are very flexible. For example, the
      <classname>HttpServletRequest</classname> can be accessed in Servlet
      environments and the <classname>PortletRequest</classname> in Portlet
      environments. The return type can be a <classname>String</classname>,
      which is interpreted as a view name or a
2741
      <classname>ModelAndView</classname> object. Refer to the API
2742 2743
      documentation for more details.</para>
    </section>
2744 2745 2746
  </section>

  <section id="mvc-coc">
2747
    <title>Convention over configuration support</title>
2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759

    <para>For a lot of projects, sticking to established conventions and
    having reasonable defaults is just what they (the projects) need... this
    theme of convention-over-configuration now has explicit support in Spring
    Web MVC. What this means is that if you establish a set of naming
    conventions and suchlike, you can <emphasis>substantially</emphasis> cut
    down on the amount of configuration that is required to set up handler
    mappings, view resolvers, <classname>ModelAndView</classname> instances,
    etc. This is a great boon with regards to rapid prototyping, and can also
    lend a degree of (always good-to-have) consistency across a codebase
    should you choose to move forward with it into production.</para>

2760 2761
    <para>Convention-over-configuration support addresses the three core areas
    of MVC -- models, views, and controllers.</para>
2762 2763

    <section id="mvc-coc-ccnhm">
2764
      <title>The Controller
2765 2766 2767 2768 2769 2770 2771 2772
      <classname>ControllerClassNameHandlerMapping</classname></title>

      <para>The <classname>ControllerClassNameHandlerMapping</classname> class
      is a <interfacename>HandlerMapping</interfacename> implementation that
      uses a convention to determine the mapping between request URLs and the
      <interfacename>Controller</interfacename> instances that are to handle
      those requests.</para>

2773 2774 2775
      <para>Consider the following simple
      <interfacename>Controller</interfacename> implementation. Take special
      notice of the <emphasis>name</emphasis> of the class.<!--Re preceding sentence, I don't see where the name of the class is discussed in explanation following the example. See my next comment.--></para>
2776

2777
      <programlisting language="java">public class <emphasis role="bold">ViewShoppingCartController</emphasis> implements Controller {
2778 2779 2780 2781 2782 2783 2784 2785 2786

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
        <lineannotation>// the implementation is not hugely important for this example...</lineannotation>
    }
}</programlisting>

      <para>Here is a snippet from the attendent Spring Web MVC configuration
      file...</para>

2787
      <programlisting language="xml">&lt;bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/&gt;
2788 2789 2790 2791 2792 2793 2794 2795
                
&lt;bean id="<emphasis role="bold">viewShoppingCart</emphasis>" class="x.y.z.ViewShoppingCartController"&gt;
    <lineannotation>&lt;!-- inject dependencies as required... --&gt;</lineannotation>
&lt;/bean&gt;</programlisting>

      <para>The <classname>ControllerClassNameHandlerMapping</classname> finds
      all of the various handler (or
      <interfacename>Controller</interfacename>) beans defined in its
2796 2797 2798 2799
      application context and strips <literal>Controller</literal> off the
      name to define its handler mappings. Thus,
      <classname>ViewShoppingCartController</classname> maps to the
      <literal>/viewshoppingcart*</literal> request URL.</para>
2800 2801

      <para>Let's look at some more examples so that the central idea becomes
2802 2803 2804
      immediately familiar. (Notice all lowercase in the URLs, in contrast to
      camel-cased <interfacename>Controller</interfacename> class
      names.)</para>
2805 2806 2807 2808

      <itemizedlist>
        <listitem>
          <para><classname>WelcomeController</classname> maps to the
2809
          <literal>/welcome*</literal> request URL</para>
2810 2811 2812 2813
        </listitem>

        <listitem>
          <para><classname>HomeController</classname> maps to the
2814
          <literal>/home*</literal> request URL</para>
2815 2816 2817 2818
        </listitem>

        <listitem>
          <para><classname>IndexController</classname> maps to the
2819
          <literal>/index*</literal> request URL</para>
2820 2821 2822 2823
        </listitem>

        <listitem>
          <para><classname>RegisterController</classname> maps to the
2824
          <literal>/register*</literal> request URL</para>
2825 2826 2827 2828
        </listitem>
      </itemizedlist>

      <para>In the case of <classname>MultiActionController</classname>
2829 2830 2831 2832
      handler classes, the mappings generated are slightly more complex. The
      <interfacename>Controller</interfacename> names in the following
      examples are assumed to be <classname>MultiActionController</classname>
      implementations:</para>
2833 2834 2835 2836

      <itemizedlist>
        <listitem>
          <para><classname>AdminController</classname> maps to the
S
Sam Brannen 已提交
2837
          <literal>/admin</literal><emphasis role="bold">/*</emphasis> request
2838
          URL</para>
2839 2840 2841 2842
        </listitem>

        <listitem>
          <para><classname>CatalogController</classname> maps to the
S
Sam Brannen 已提交
2843
          <literal>/catalog</literal><emphasis role="bold">/*</emphasis>
2844 2845 2846 2847
          request URL</para>
        </listitem>
      </itemizedlist>

2848
      <para>If you follow the convention of naming your
2849
      <interfacename>Controller</interfacename> implementations as
S
Sam Brannen 已提交
2850
      <literal>xxx</literal><emphasis role="bold">Controller</emphasis>, the
2851 2852 2853
      <classname>ControllerClassNameHandlerMapping</classname> saves you the
      tedium of defining and maintaining a potentially
      <emphasis>looooong</emphasis>
2854 2855 2856 2857 2858
      <classname>SimpleUrlHandlerMapping</classname> (or suchlike).</para>

      <para>The <classname>ControllerClassNameHandlerMapping</classname> class
      extends the <classname>AbstractHandlerMapping</classname> base class so
      you can define <interfacename>HandlerInterceptor</interfacename>
2859
      instances and everything else just as you would with many other
2860 2861 2862 2863
      <interfacename>HandlerMapping</interfacename> implementations.</para>
    </section>

    <section id="mvc-coc-modelmap">
2864
      <title>The Model <classname>ModelMap</classname>
2865 2866 2867 2868 2869 2870 2871 2872 2873
      (<classname>ModelAndView</classname>)</title>

      <para>The <classname>ModelMap</classname> class is essentially a
      glorified <interfacename>Map</interfacename> that can make adding
      objects that are to be displayed in (or on) a
      <interfacename>View</interfacename> adhere to a common naming
      convention. Consider the following
      <interfacename>Controller</interfacename> implementation; notice that
      objects are added to the <classname>ModelAndView</classname> without any
2874
      associated name specified.</para>
2875

2876
      <programlisting language="java">public class DisplayShoppingCartController implements Controller {
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
        
        List cartItems = <lineannotation>// get a <interfacename>List</interfacename> of <classname>CartItem</classname> objects</lineannotation>
        User user = <lineannotation>// get the <classname>User</classname> doing the shopping</lineannotation>
        
        ModelAndView mav = new ModelAndView("displayShoppingCart"); <lineannotation>&lt;-- the logical view name</lineannotation>

        mav.addObject(cartItems); <lineannotation>&lt;-- look ma, no name, just the object</lineannotation>
        mav.addObject(user); <lineannotation>&lt;-- and again ma!</lineannotation>

        return mav;
    }
}</programlisting>

      <para>The <classname>ModelAndView</classname> class uses a
      <classname>ModelMap</classname> class that is a custom
      <interfacename>Map</interfacename> implementation that automatically
      generates a key for an object when an object is added to it. The
      strategy for determining the name for an added object is, in the case of
      a scalar object such as <classname>User</classname>, to use the short
2898 2899
      class name of the object's class. The following examples are names that
      are generated for scalar objects put into a
2900 2901 2902 2903 2904
      <classname>ModelMap</classname> instance.</para>

      <itemizedlist>
        <listitem>
          <para>An <classname>x.y.User</classname> instance added will have
2905
          the name <literal>user</literal> generated.</para>
2906 2907 2908 2909
        </listitem>

        <listitem>
          <para>An <classname>x.y.Registration</classname> instance added will
2910
          have the name <literal>registration</literal> generated.</para>
2911 2912 2913 2914
        </listitem>

        <listitem>
          <para>An <classname>x.y.Foo</classname> instance added will have the
2915
          name <literal>foo</literal> generated.</para>
2916 2917 2918 2919
        </listitem>

        <listitem>
          <para>A <classname>java.util.HashMap</classname> instance added will
2920 2921 2922
          have the name <literal>hashMap</literal> generated. You probably
          want to be explicit about the name in this case because
          <literal>hashMap</literal> is less than intuitive.</para>
2923 2924 2925 2926 2927
        </listitem>

        <listitem>
          <para>Adding <literal>null</literal> will result in an
          <classname>IllegalArgumentException</classname> being thrown. If the
2928
          object (or objects) that you are adding could be
2929
          <literal>null</literal>, then you will also want to be explicit
2930
          about the name.</para>
2931 2932 2933 2934 2935 2936
        </listitem>
      </itemizedlist>

      <sidebar>
        <title>What, no automatic pluralisation?</title>

2937 2938
        <para>Spring Web MVC's convention-over-configuration support does not
        support automatic pluralisation. That is, you cannot add a
2939 2940
        <interfacename>List</interfacename> of <classname>Person</classname>
        objects to a <classname>ModelAndView</classname> and have the
2941
        generated name be <classname>people</classname>.</para>
2942

2943
        <para>This decision was made after some debate, with the
2944 2945 2946 2947 2948 2949 2950 2951
        <quote>Principle of Least Surprise</quote> winning out in the
        end.</para>
      </sidebar>

      <para>The strategy for generating a name after adding a
      <interfacename>Set</interfacename>, <interfacename>List</interfacename>
      or array object is to peek into the collection, take the short class
      name of the first object in the collection, and use that with
2952
      <literal>List</literal> appended to the name. Some examples will make
2953 2954 2955 2956 2957 2958
      the semantics of name generation for collections clearer...</para>

      <itemizedlist>
        <listitem>
          <para>An <classname>x.y.User[]</classname> array with one or more
          <classname>x.y.User</classname> elements added will have the name
2959
          <literal>userList</literal> generated.</para>
2960 2961 2962 2963 2964
        </listitem>

        <listitem>
          <para>An <classname>x.y.Foo[]</classname> array with one or more
          <classname>x.y.User</classname> elements added will have the name
2965
          <literal>fooList</literal> generated.</para>
2966 2967 2968 2969 2970
        </listitem>

        <listitem>
          <para>A <classname>java.util.ArrayList</classname> with one or more
          <classname>x.y.User</classname> elements added will have the name
2971
          <literal>userList</literal> generated.</para>
2972 2973 2974 2975 2976
        </listitem>

        <listitem>
          <para>A <classname>java.util.HashSet</classname> with one or more
          <classname>x.y.Foo</classname> elements added will have the name
2977
          <literal>fooList</literal> generated.</para>
2978 2979 2980 2981 2982
        </listitem>

        <listitem>
          <para>An <emphasis role="bold">empty</emphasis>
          <classname>java.util.ArrayList</classname> will not be added at all
2983
          (in effect, the <methodname>addObject(..)</methodname> call will
2984 2985 2986 2987 2988 2989 2990 2991 2992 2993
          essentially be a no-op).</para>
        </listitem>
      </itemizedlist>
    </section>

    <section id="mvc-coc-r2vnt">
      <title>The View -
      <interfacename>RequestToViewNameTranslator</interfacename></title>

      <para>The <interfacename>RequestToViewNameTranslator</interfacename>
2994 2995 2996
      interface determines a logical <interfacename>View</interfacename> name
      when no such logical view name is explicitly supplied. It has just one
      implementation, the
2997 2998 2999
      <classname>DefaultRequestToViewNameTranslator</classname> class.</para>

      <para>The <classname>DefaultRequestToViewNameTranslator</classname> maps
3000
      request URLs to logical view names, as with this example:</para>
3001

3002
      <programlisting language="java">public class RegistrationController implements Controller {
3003 3004 3005 3006 3007 3008 3009 3010 3011 3012
                
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
        <lineannotation>// process the request...</lineannotation>
        ModelAndView mav = new ModelAndView();
        <lineannotation>// add <emphasis role="bold">data</emphasis> as necessary to the model...</lineannotation>
        return mav;
        <lineannotation>// notice that no <interfacename>View</interfacename> or logical view name has been set</lineannotation>
    }
}</programlisting>

3013
      <programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt;
&lt;beans&gt;

    <lineannotation>&lt;!-- this bean with the well known name generates view names for us --&gt;</lineannotation>
    &lt;bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/&gt;

    &lt;bean class="x.y.RegistrationController"&gt;
        <lineannotation>&lt;!-- inject dependencies as necessary --&gt;</lineannotation>
    &lt;/bean&gt;
    
    <lineannotation>&lt;!-- maps request URLs to Controller names --&gt;</lineannotation>
    &lt;bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/&gt;

    &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
        &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
        &lt;property name="suffix" value=".jsp"/&gt;
    &lt;/bean&gt;

&lt;/beans&gt;
</programlisting>

      <para>Notice how in the implementation of the
      <literal>handleRequest(..)</literal> method no
      <interfacename>View</interfacename> or logical view name is ever set on
3039 3040 3041 3042 3043
      the <classname>ModelAndView</classname> that is returned. The
      <classname>DefaultRequestToViewNameTranslator</classname> is tasked with
      generating a <emphasis>logical view name</emphasis> from the URL of the
      request. In the case of the above
      <classname>RegistrationController</classname>, which is used in
3044 3045
      conjunction with the
      <classname>ControllerClassNameHandlerMapping</classname>, a request URL
3046 3047 3048 3049 3050
      of <literal>http://localhost/registration.html</literal> results in a
      logical view name of <literal>registration</literal> being generated by
      the <classname>DefaultRequestToViewNameTranslator</classname>. This
      logical view name is then resolved into the
      <literal>/WEB-INF/jsp/registration.jsp</literal> view by the
3051 3052 3053
      <classname>InternalResourceViewResolver</classname> bean.</para>

      <tip>
3054
        <para>You do not need to define a
3055
        <classname>DefaultRequestToViewNameTranslator</classname> bean
3056 3057 3058 3059 3060
        explicitly. If you like the default settings of the
        <classname>DefaultRequestToViewNameTranslator</classname>, you can
        rely on the Spring Web MVC <classname>DispatcherServlet</classname> to
        instantiate an instance of this class if one is not explicitly
        configured.</para>
3061 3062 3063 3064 3065
      </tip>

      <para>Of course, if you need to change the default settings, then you do
      need to configure your own
      <classname>DefaultRequestToViewNameTranslator</classname> bean
3066
      explicitly. Consult the comprehensive Javadoc for the
3067 3068 3069 3070 3071
      <classname>DefaultRequestToViewNameTranslator</classname> class for
      details of the various properties that can be configured.</para>
    </section>
  </section>

3072 3073 3074 3075 3076 3077 3078 3079 3080 3081
  <section id="mvc-etag">
    <title>ETag support</title>

    <para>An <ulink url="http://en.wikipedia.org/wiki/HTTP_ETag">ETag</ulink>
    (entity tag) is an HTTP response header returned by an HTTP/1.1 compliant
    web server used to determine change in content at a given URL. It can be
    considered to be the more sophisticated successor to the
    <literal>Last-Modified</literal> header. When a server returns a
    representation with an ETag header, the client can use this header in
    subsequent GETs, in an <literal>If-None-Match</literal> header. If the
3082
    content has not changed, the server returns <literal>304: Not
3083 3084 3085
    Modified</literal>.</para>

    <para>Support for ETags is provided by the servlet filter
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098
    <classname>ShallowEtagHeaderFilter</classname>. It is a plain Servlet
    Filter, and thus can be used in combination with any web framework. <!--The preceding sentence was a fragment, not a complete sentence. Have I reworded ok?-->The
    <classname>ShallowEtagHeaderFilter</classname> filter creates so-called
    shallow ETags (as opposed to deep ETags, more about that later).<!--Provide xref to deep ETags.-->The
    filter caches the content of the rendered JSP (or other content),
    generates an MD5 hash over that, and returns that as an ETag header in the
    response. The next time a client sends a request for the same resource, it
    uses that hash as the <literal>If-None-Match</literal> value. The filter
    detects this, renders the view again, and compares the two hashes. If they
    are equal, a <literal>304</literal> is returned. This filter will not save
    processing power, as the view is still rendered. The only thing it saves
    is bandwidth, as the rendered response is not sent back over the
    wire.</para>
3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113

    <para>You configure the <classname>ShallowEtagHeaderFilter</classname> in
    <filename>web.xml</filename>:</para>

    <programlisting language="xml">&lt;filter&gt;
  &lt;filter-name&gt;etagFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;org.springframework.web.filter.ShallowEtagHeaderFilter&lt;/filter-class&gt;
&lt;/filter&gt;

&lt;filter-mapping&gt;
  &lt;filter-name&gt;etagFilter&lt;/filter-name&gt;
  &lt;servlet-name&gt;petclinic&lt;/servlet-name&gt;
&lt;/filter-mapping&gt;</programlisting>
  </section>

3114
  <section id="mvc-resources">
3115
    <title>More Spring Web MVC Resources</title>
3116

3117 3118
    <para>See the following links and pointers for more resources about Spring
    Web MVC:</para>
3119 3120 3121 3122 3123 3124

    <itemizedlist>
      <listitem>
        <para>The Spring distribution ships with a Spring Web MVC tutorial
        that guides the reader through building a complete Spring Web
        MVC-based application using a step-by-step approach. This tutorial is
3125
        available in the <literal>docs</literal> directory of the Spring
3126
        distribution. An online version can also be found on the <ulink
3127 3128
        url="http://springframework.org/">Spring Framework
        website</ulink>.</para>
3129 3130 3131
      </listitem>

      <listitem>
3132 3133 3134
        <para><quote>Expert Spring Web MVC and Web Flow</quote> by Seth Ladd
        and others (published by Apress) is an excellent hard copy source of
        Spring Web MVC goodness.</para>
3135 3136 3137
      </listitem>
    </itemizedlist>
  </section>
3138
</chapter>