Spring MVC Revisited

Recently I had an opportunity to play with architecture of a new internal app in my company. In order to properly do my job I’ve reread whole Spring MVC documentation. It was very refreshing ( after a while of using this framework ) to discover several new things. My goal is just to present short list of features with links to docs. There is no point in reinventing the wheel 😉

Matrix Params

Ever wanted to provide more data with just URI? Matrix variables are the best way to do this. Just look at the URI presented:


Yep, you can process it and extract data very easilly. Above URI and nice blog posts about them can be found on Captain’s Debug blog (second part with code snippets here).


I am used to using ModelAttribute annotation, mostly on update/save actions in my controllers. What I did not know is that this annotation can be also used with methods. Remember all the situations when You must fill several model variables with data just in order to render them as eg. SELECT-tag values? Now you create just one method in controller and annotate it with @ModelAttribute to put method return value in model. More information can be found in spring docs.

Flash attributes with redirects

I remember problems with simple flash redirects a few years back (circa early Spring’s version 3, flash attributes were introduced in 3.1). Today they are even better handled – You do not need to manually put them into FlashMap. There is a nice class RedirectAttributes which greatly improves coders’ quality of life. Just look at this code:

@RequestMapping(value = "/accounts", method = RequestMethod.POST)
 public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
   if (result.hasErrors()) {
     return "accounts/new";
   // Save account ...
   redirectAttrs.addAttribute("id", account.getId()).addFlashAttribute("message", "Account created!");
   return "redirect:/accounts/{id}";

Above example comes from RedirectAttributes API, but there is also short Spring doc section and simple tutorial.

BindingResult order

There is nothing new in BindingResult interface. But what cought my attention is the need for this interface be declared as first parameter after @ModelAttribute in controller’s method to work!. I was not aware of that and I assume knowing that can be real time-saver. Short example:

public String addNewBlogger(@ModelAttribute("blogger") Blogger blogger,
            ModelMap map, BindingResult results) 

The above example will not work properly. You will get either exception or application’s logic will behave strangely (depends mostly on error handling and view layer/resolver). Proper solution look like this:

public String addNewBlogger(@ModelAttribute("blogger") Blogger blogger, BindingResult results, ModelMap map) 

Simple creation of URIs

Very often there is a need to create a new URI, eg. to perform redirect. Concatenating string values with parameters is not often clean and simple (not to mention it is very error prone). In order to ease developers’ life Spring provides very dsl-like solution with MvcUriComponentsBuilder class. More can be found in documentation.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s