Announcement Announcement Module
No announcement yet.
Better error messages when relying on Spring to bind Integer values Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Better error messages when relying on Spring to bind Integer values

    I like Spring webmvc's convenient binding mechanism. But some fields take an Integer or Short instead of a String where users have to enter numbers in a textfield. For example when allowing a user to specify a year (with thousands of possibilities). If a user enters "abcd", this is what they see as an error message from Spring output to the <form:errors path="year"/> tag:

    Failed to convert property value of type [java.lang.String] to required type [java.lang.Short] for property year; nested exception is java.lang.NumberFormatException: For input string: &quot;abcd&quot;

    That's obviously pretty ugly... how do I make that pretty?

  • #2
    Have a look at this thread I think you will find the answer in there


    • #3
      Excellent! typeMismatch is the message code prefix that handles this. Thanks Krycek!! Why isn't this in the manual?! Seems like a pretty major feature. The MessageCodeResolver javadoc is not very clear about this.


      • #4
        I was asking myself the same question I am glad there is a big community, so we can all help eachother when needed


        • #5
          I remember this was my first question on the forums a couple of years ago, and I have seen it come up literally hundreds of times since then!

          I just looked at the manual ( and although it is covered, I can see why people find it confusing or miss it all together.