Configuring Spring MVC - Java-Springs

Spring 3 introduces a mvc XML configuration namespace that simplifies the setup of Spring MVC inside your web application. Instead of registering low-level beans such as Annotation Method Handler Adapter,you can simply use the namespace and its higher-level constructs.This is generally preferred unless you require finer-grained control of the configuration at the bean level. The mvc namespace consists of three tags: mvc:annotation-driven, mvc:interceptors, and mvc:view-controller.Each of these tags is documented below and in the XML schema.

mvc:annotation-driven

This tag registers the Default Annotation Handler Mapping and Annotation Method Handler Adapter beans that are required for Spring MVC to dispatch requests to @Controllers.The tag configures those two beans with sensible defaults based on what is present in your classpath. The defaults are:

  1. Support for Spring 3's Type Conversion Service in addition to JavaBeans Property Editors during Data Binding. A Conversion Service instance produced by the org. spring frame work. format. support. Formatting Conversion Service Factory Bean is used by default. This can be overriden by setting the conversion-service attribute.
  2. Support for formatting Number fields using the @NumberFormat annotation.
  3. Support for formatting Date, Calendar, Long, and Joda Time fields using the @Date Time Format annotation, if Joda Time 1.3 or higher is present on the classpath.
  4. Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath. The validation system can be explicitly configured by setting the validator attribute.
  5. Support for reading and writing XML, if JAXB is present on the classpath.
  6. Support for reading and writing JSON, if Jackson is present on the classpath.

mvc:interceptors

This tag allows you to register custom Handler Interceptors or Web Request Interceptors that should be applied to all Handler Mapping beans.You can also restrict the URL paths specifc interceptors apply to. An example of registering an interceptor applied to all URL paths:

<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>

An example of registering an interceptor limited to a specific URL path:

<mvc:interceptors>
<mvc:interceptor>
<mapping path="/secure/*"/>
<bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

mvc:view-controller

This tag is a shorcut for defining a Parameterizable View Controller that immediately forwards to a view when invoked. Use it in static cases when there is no Java Controller logic to execute before the view generates the response.

An example of view-controller that forwards to a home page is shown below:

<mvc:view-controller path="/" view-name="home"/>

mvc:resources

This tag allows static resource requests following a particular URL pattern to be served by a Resource Http Request Handler from any of a list of Resource locations. This provides a convenient way to serve static resources from locations other than the web application root, including locations on the classpath, and the resources are served with far future expiration headers (1 year, as recommended by optimization tools such as Page Speed and YSlow) so that they will be more efficiently utilized by the client. For example, to serve resource requests with a URL pattern of /resources/** from a public-resources directory within the web application root, the tag would be used as follows:

<mvc:resources mapping="/resources/**" location="/public-resources/"/>

The mapping attribute must be an Ant pattern that can be used by SimpleUrlHandlerMapping, and the location attribute must specify one or more valid resource directory locations. Multiple resource locations may be specified using a comma-seperated list of values. The locations specified will be checked in the specified order for the presence of the resource for any given request. For example, to enable the serving of resources from both the web application root and from a known path of /META-INF/public-web-resources/ in any jar on the classpath, the tag would be specified as:

<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/public-web-resources/"/>

When serving resources that may change when a new version of the application is deployed, it is recommended that you incorporate a version string into the mapping pattern used to request the resources, so that you may force clients to request the newly deployed version of your application's resources. Such a version string can be parameterized and accessed using SpEL so that it may be easily managed in a single place when deploying new versions.

As an example, let's consider an application that uses a performance-optimized custom build (as recommended) of the Dojo Java Script library in production, and that the build is generally deployed within the web application at a path of /public-resources/dojo/dojo.js. Since different parts of Dojo may be incorporated into the custom build for each new version of the application, the client web browsers need to be forced to re-download that custom-built dojo.js resource any time a new version of the application is deployed. A simple way to achieve this would be to manage the version of the application in a properties file, such as:

application.version=1.0.0

and then to make the properties file's values accessible to SpEL as a bean using the util:properties tag:

<util:properties id="applicationProps" location="/WEB-INF/spring/application.properties"/>

With the application version now accessible via SpEL, we can incorporate this into the use of the resources tag:

<mvc:resources mapping="/resources-#{applicationProps['application.version']}
/**" location="/public-resources/"/>

and finally, to request the resource with the proper URL, we can take advantage of the Spring JSP tags:

<spring:eval expression="@applicationProps['application.version']" var="applicationVersion"/>
<spring:url value="/resources-{applicationVersion}" var="resourceUrl">
<spring:param name="applicationVersion" value="${applicationVersion}"/>
</spring:url>
<script src="${resourceUrl}/dojo/dojo.js" type="text/javascript"> </script>

mvc:default-servlet-handler

This tag allows for mapping the Dispatcher Servlet to "/"(thus overriding the mapping of the container's default Servlet), while still allowing static resource requests to be handled by the container's default Servlet. It configures a DefaultServletHttpHandler with a URL mapping (given a lowest precedence order) of "/**". This handler will forward all requests to the default Servlet.To enable this feature using the default setup, simply include the tag in the form: <mvc:default-servlet-handler/>

The caveat to overriding the "/" Servlet mapping is that the Request Dispatcher for the default Servlet must be retrieved by name rather than by path. The Default Servlet Http Handler will attempt to auto-detect the default Servlet for the container at startup time, using a list of known names for most of the major Servlet containers (including Tomcat, Jetty, Glassfish, JBoss, Resin, WebLogic, and Web Sphere).If the default Servlet has been custom configured with a different name, or if a different Servlet container is being used where the default Servlet name is unknown, then the default Servlet's name must be explicitly provided as in the following example:

<mvc:default-servlet-handler default-servlet-name="myCustomDefaultServlet"/>

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java-Springs Topics