Spring: How to resolve a validation error -> error code -> error message

11,583

You are, as you guessed, making it way harder on yourself than it needs to be. The FieldError object is itself a MessageSourceResolvable. You don't need to get the codes off of it then take individual codes manually to your message source and go looking. You can just pass it to your MessageSource and it will find the most specific one that has a translation defined in your locale. (assuming your code resolver put them on in the right order.)

You really don't even need to do that in most cases though. Putting the Errors on your backing object and translating them yourself isn't usually needed. The form namespace in the jsp library provides a tag that looks up error messages for you. All you need to do is put the Errors in the ModelMap. See docs:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html#view-jsp-formtaglib-errorstag

Share:
11,583
David Parks
Author by

David Parks

Merge Keep

Updated on June 13, 2022

Comments

  • David Parks
    David Parks almost 2 years

    In Spring, after validation we get a BindingResult object in the controller.

    Simple enough, if I get validation errors I want to re-display my form with the error message above each afflicted field.

    So to check for field errors on field username of my FormObject I call:

    FieldError usernameFieldError = bindingResult.getFieldError("username");
    

    Great, now I hold a FieldError object which, assuming I'm using the DefaultMessageCodeResolver now contains something like 4 possible error codes.

    How do I go from FieldError -> A string that is consumable to the user?

    I have a MessageSource defined in my webapplication context, so I can map a single error code to a message.

    But sometimes the default message will be best, and sometimes I expect that two of the error codes might have a relevant message, so we need to choose the best one.

    What method do I use to determine the best possible error message to present for a field error?

    • Do I need to write some algorithm to go through all the error codes and pick from the most specific?
    • Does spring provide any support for helping determine the most specific error message?
    • This whole process seems so long and convoluted, I thought spring was supposed to make this stuff easy. Maybe I'm totally off base somehow?
  • David Parks
    David Parks over 13 years
    Oooh, I see, I didn't catch MessageSource.get(MessageSoureResolvable, ...), I just noticed the get(String, ...) options, that's the key link (actually I probably noticed that and lost it in the myriad of details that we get lost in when learning a new framework/api). Thanks!!
  • David Parks
    David Parks over 13 years
    Interesting point about the JSP taglib, I'm using Velocity, but Spring offers the same in macro form. But I haven't employed it yet as it seemed rather anti-spring to tie even my view presentation to the spring framework. I thought it bad enough that my controller now calls the spring-specific MessageSource rather than a java-standard ResourceBundle. But maybe I'm overthinking it? It's my first time out with Spring.
  • Affe
    Affe over 13 years
    True enough, but as far as view technologies go, you're generally going to end up coupled to something. You'll do a lot of reinventing the wheel if you try to use only JSTL and the servlet API! WebMVC and WebFlow are fairly nonintrusive, and with the tag libraries you could fairly easily create your own .tag files that wrap them and 'isolate' the dependency to a single place that the implementation could later be changed. Using the tag would also remove the dependency on the Errors interface in your backing object, allowing you to use a spring-free pojo.