Announcement Announcement Module
Collapse
No announcement yet.
Better error messages when relying on Spring to bind Integer values Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 http://forum.springframework.org/showthread.php?t=57281. I think you will find the answer in there

    Comment


    • #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.

      Comment


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

        Comment


        • #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 (http://static.springframework.org/sp...ion-conversion) and although it is covered, I can see why people find it confusing or miss it all together.

          Comment

          Working...
          X