Tag Archives: Spring MVC

S2G Conference, London May 31st – Notes on Spring MVC 3.1, Rossen Stoyanchev

Java based Web MVC Configuration
Contimizable @MVC
URI templates variable
Serverside testing

Java based Web Annotation Namespace
Java based config allows you to create  a class, annotate it with a @configuration to indicates that this is going to created bean instances. This allows you to keep your config with your code
Most Spring MVC is java based already so why not enhance it. Servlet 3.0 spec further reduces the xml load and xml is not transparent anyway.

@EnableWebMvc
Enables declarations of HandlerMapping, Converters, Resolvers, MessageSources, Multiplart

@Configuration
@EnableWebMvc
public class WebCnfig {

}

Also have the ability to exclude configuration classes that have already been discovered

//WebMvcConfiguration example class already exists but also extending WebMvcConfigurationAdaptor allows selective overrides for easier start up.

This does not exclude xml based config. It just means that Java is loaded first and then xml can be loaded secondarily

An example of this os the  GreenHouse application – the WebConfig.java class.

Nice use of the Environment object which plays nicely with the profiles. The properties are are enabled with the code

Cusomtizable MVC
The chance to annotation based MVC was a big change. More flexible mapping, better information on interception

New classes. HandlerMethod not just for @RequestMapping but also @InitBinder, @ModelAttribute and @ExceptionHandler. These underlying infrastructure classes have numerous advantages. THe ability to know the handler method that is going to be invoked, different controller has handler different HTTP methods on the same URL

Fully customisable processing pf all method arguments – HandlerMethodArgumentResolver
Same of all return types
HandlerMethodRetutnValueHandler

Transition from 3.0 should be seeless since all the current classes will still exist but the new classes are the future and are recommended

Handler Method Interception
Pr/post bind and pre/post validation hooks
COntrollerethod name conventions for default views, e.g. Account.show = account/show

URI Template Variable
Path variables can be automatically added to the model
Redirect supports path variables, e.g. RequestMapping /events/{year}/{month} has a redirect of “redirect:/return/events/{year}”{month}
It also offers databinding on none model parameters in the request mapping decorated methods

Matching MediaTypes
In 3.0 you used the headers attribute in the @RequestMapping annotation. In 3.1 the consumers and produces similar to JAX-RS. The consumers/produces offers the benefits of a proper return value on mismatches like 415 on a bad request type or 406 NOT_ACCEPTABLE

Serverside Test Support.
Testing a controller properly. Unit test only does the class and not the framework features. This normally requires an integration test that requires an in-memory  servlet  (JWebUnit)
We want to verifier framework features like binding errors or redirects and forwards or even view names
This is work in progress and will be in the spring-test project when it is ready