Discovering Spring MVC

Spring MVC is not taken seriously jet because of almost absent documentation. For 2.0 version they wrote couple more paragraphs. But still only pages 214-250 devoted to Spring MVC itself. Many features left undocumented.

For my recent project I had to build a form having 6 different sets of fields. Depending on selected value in combo-box I process specific set. The rest of fields are ignored. That was easy to decide but how to tell Spring no to bind some fields?

One approach is to process input manually disabling automatic binding by overriding supressBinding function:

protected boolean suppressBinding(HttpServletRequest request) {
  return false;

Will work but leaves whole burden of binding on your shoulders. You do not want that. The other approach is letting Spring know which fields you want to process and which not. Take a look:

protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
  super.initBinder(request, binder);
  if (! isFormSubmission(request))
    return ;
  try {
    String selectedBenefitType = ServletRequestUtils.getRequiredStringParameter(request, "benefitType");
    List disallowedFields = new ArrayList();
    //disallow processing of any field that do not belong to currently selected benefit
    for (String benefitType:BaseBenefit.benefitTypes){
      if (! selectedBenefitType.equalsIgnoreCase(benefitType)){
        String fieldMask = StringUtils.uncapitalize(benefitType)+".*";
    binder.setDisallowedFields(disallowedFields.toArray(new String[disallowedFields.size()]));
    binder.setRequiredFields(new String[]{"benefitType", "sinceDate", "untilDate"});
  } catch (ServletRequestBindingException e) {
    logger.error(e.getMessage(), e);
    throw new RuntimeException(e);

Here we pass list of unwanted fields to binder.setDisallowedFields and that is it. Those fields will not be bound and you will not receive ‘Can not convert field someDate to java.util.Date’ for fields that are not wanted, not filled and even hidden on the form. As you have probably noticed You can use wild-cards in field names.

I like Spring a lot. And I use it in every single Java project I do. So take my advice. When you want to use Spring MVC seriously, the first step is to tear away Spring MVC paragraph from documentation so you do not pretend that you have it at all. Read an API and dig into source code. Soon you will discover that your web-tier improved a lot.

4 Responses to “Discovering Spring MVC”

  1. Kamal Mettananda Says:

    I too used to have the spring source with me and do some debugging when ever needed. Spring forum is active and that helps a lot.

  2. Jane Goody Says:

    This is quite a up-to-date information. I’ll share it on Digg.

  3. Mike Pun Says:


    you are so right. I am a java newbie, but I have been able to do a good amount with Spring MVC. My problem is Spring Portlet MVC. It seem like a great tool, but I cannot configure eclipse and pluto or anything so that I can debug or deploy portlets. Can’t, for the life of me, find any documentation on this. Do you have any insight?

  4. Professional Web Hosting from Just Host Says:

    Great blog! Do you have any helpful hints for aspiring writers?

    I’m planning to start my own website soon but I’m a little lost on everything.
    Would you advise starting with a free platform like WordPress or go for a paid option?
    There are so many choices out there that I’m completely overwhelmed ..
    Any tips? Thanks a lot!

Leave a Reply

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

You are commenting using your 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

<span>%d</span> bloggers like this: