Skip to content

Commit bfd61d6

Browse files
author
Keith Donald
committed
mvc namespace docs
1 parent 61958b6 commit bfd61d6

File tree

1 file changed

+110
-0
lines changed
  • spring-framework-reference/src

1 file changed

+110
-0
lines changed

spring-framework-reference/src/mvc.xml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,7 +3119,117 @@ public class SimpleController {
31193119
<servlet-name>petclinic</servlet-name>
31203120
&lt;/filter-mapping&gt;</programlisting>
31213121
</section>
3122+
<section id="mvc-config">
3123+
<title>Configuring Spring MVC</title>
3124+
<para>
3125+
Spring 3 introduces a <literal>mvc</literal> XML configuration namespace that simplifies the setup of Spring MVC inside your web application.
3126+
Instead of registering low-level beans such as AnnotationMethodHandlerAdapter, you can simply use the namespace and its higher-level constructs.
3127+
This is generally preferred unless you require finer-grained control of the configuration at the bean level.
3128+
</para>
3129+
<para>
3130+
The mvc namespace consists of three tags: mvc:annotation-driven, mvc:interceptors, and mvc:view-controller.
3131+
Each of these tags is documented below and in the <ulink url="http://static.springsource.org/schema/mvc/spring-mvc-3.0.xsd">XML schema</ulink>.
3132+
</para>
3133+
<section id="mvc-annotation-driven">
3134+
<title>mvc:annotation-driven</title>
3135+
<para>
3136+
This tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC to dispatch requests to @Controllers.
3137+
The tag configures those two beans with sensible defaults based on what is present in your classpath.
3138+
The defaults are:
3139+
<orderedlist>
3140+
<listitem>
3141+
<para>
3142+
Support for Spring 3's Type <link linkend="core-convert">ConversionService</link> in addition to JavaBeans PropertyEditors during Data Binding.
3143+
A ConversionService instance produced by the <classname>org.springframework.format.support.FormattingConversionServiceFactoryBean</classname> is used by default.
3144+
This can be overriden by setting the <literal>conversion-service</literal> attribute.
3145+
</para>
3146+
</listitem>
3147+
<listitem>
3148+
<para>
3149+
Support for <link linkend="format">formatting</link> Number fields using the @NumberFormat annotation
3150+
</para>
3151+
</listitem>
3152+
<listitem>
3153+
<para>
3154+
Support for <link linkend="format">formatting</link> Date, Calendar, Long, and Joda Time fields using the @DateTimeFormat annotation, if Joda Time is present on the classpath.
3155+
</para>
3156+
</listitem>
3157+
<listitem>
3158+
<para>
3159+
Support for <link linkend="validation-mvc-jsr303">validating</link> @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.
3160+
The validation system can be explicitly configured by setting the <literal>validator</literal> attribute.
3161+
</para>
3162+
</listitem>
3163+
<listitem>
3164+
<para>
3165+
Support for reading and writing XML, if JAXB is present on the classpath.
3166+
</para>
3167+
</listitem>
3168+
<listitem>
3169+
<para>
3170+
Support for reading and writing JSON, if Jackson is present on the classpath.
3171+
</para>
3172+
</listitem>
3173+
</orderedlist>
3174+
A typical usage is shown below:
3175+
<programlisting language="xml"><![CDATA[
3176+
<?xml version="1.0" encoding="UTF-8"?>
3177+
<beans xmlns="http://www.springframework.org/schema/beans"
3178+
xmlns:mvc="http://www.springframework.org/schema/mvc"
3179+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3180+
xsi:schemaLocation="
3181+
http://www.springframework.org/schema/beans
3182+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
3183+
http://www.springframework.org/schema/mvc
3184+
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
31223185
3186+
<!-- JSR-303 support will be detected on classpath and enabled automatically -->
3187+
<mvc:annotation-driven/>
3188+
3189+
</beans>]]>
3190+
</programlisting>
3191+
</para>
3192+
</section>
3193+
<section id="mvc-interceptors">
3194+
<title>mvc:interceptors</title>
3195+
<para>
3196+
This tag allows you to register custom HandlerInterceptors or WebRequestInterceptors that should be applied to all HandlerMapping beans.
3197+
You can also restrict the URL paths specifc interceptors apply to.
3198+
</para>
3199+
<para>
3200+
An example of registering an interceptor applied to all URL paths:
3201+
</para>
3202+
<programlisting language="xml"><![CDATA[
3203+
<mvc:interceptors>
3204+
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
3205+
</mvc:interceptors>]]>
3206+
</programlisting>
3207+
<para>
3208+
An example of registering an interceptor limited to a specific URL path:
3209+
</para>
3210+
<programlisting language="xml"><![CDATA[
3211+
<mvc:interceptors>
3212+
<mvc:interceptor>
3213+
<mapping path="/secure/*"/>
3214+
<bean class="org.example.SecurityInterceptor" />
3215+
</mvc:interceptor>
3216+
</mvc:interceptors>]]>
3217+
</programlisting>
3218+
</section>
3219+
<section id="mvc-view-controller">
3220+
<title>mvc:view-controller</title>
3221+
<para>
3222+
This tag is a shorcut for defining a <classname>ParameterizableViewController</classname> that immediately forwards to a view when invoked.
3223+
Use it in static cases when there is no Java Controller logic to execute before the view generates the response.
3224+
</para>
3225+
<para>
3226+
An example of view-controller that forwards to a home page is shown below:
3227+
</para>
3228+
<programlisting language="xml"><![CDATA[
3229+
<mvc:view-controller path="/" view-name="home"/>]]>
3230+
</programlisting>
3231+
</section>
3232+
</section>
31233233
<section id="mvc-resources">
31243234
<title>More Spring Web MVC Resources</title>
31253235

0 commit comments

Comments
 (0)