Menu

Errors and Editors

2003-11-10
2004-04-13
  • R.J. Lorimer

    R.J. Lorimer - 2003-11-10

    Hello,

    First off, let me say kudos on a very well designed project. I am constantly impressed by Spring framework; it just works. It feels like a warm fuzzy blanket.

    In any case, I am working on really understanding the web.servlet.mvc package, and I have hit a snag when making my first non-trivial SimpleFormController.

    I have a field of the form which internally represents a date. The date is automatically converted back and forth by a CustomDateEditor I registered in the initBinder method of my controller.

    Unfortunately, when the actual binding occurs, if the user has input garbage in the field, an exception occurs which is ever-so-nicely converted to a field error by Spring for me, so I don't even have to worry about it. Please note, this is PRE-Validator (I am not barking, Spring is). My only problem is the verbiage of the message; it is very system-level, and is not sufficient for users of my application.

    What is the approach (if any at this point) for swapping that cryptic message out for one of my own?

    Thanks, and keep up the good work!

    R.J.

     
    • Alef Arendsen

      Alef Arendsen - 2003-11-11

      Spring has some default messages for those kind of errors. When a PropertyEditor can't convert the property due to IllegalArgumentExceptions or NumberFormatException, it uses the mismatch key to try to resolve
      typeMismatch.[beanname].[propertyname],
      typeMismatch.[propertyname] or just
      typeMismatch

      So if you put (one of) those localization keys in your resourcebundle, you should be fine (use the {0}-notation from message format to get the actual user input in there...

      There's some more info on this in the JavaDoc of he following methods:

      ServletRequestDataBinder.bind() and
      DataBinder.bind()

      Hope this helps...

       
    • R.J. Lorimer

      R.J. Lorimer - 2003-11-11

      That did help, thanks - I've got it to a fairly reasonable state.

      Unfortunately, the user input isn't coming across as an argument to the message source; by the time the field error gets to the AbstractApplicationContext.getMessage(MessageSourceResolvable, Locale) method, the args collection is empty.

      It's not a big deal since the value is being placed back into the form anyway, but, just something to note.

      Thanks for your help!

       
    • cheechee

      cheechee - 2004-04-12

      hi
         i am a newbee in spring.
         my project does not use the message.properties since we are standardising to get all mesgs from the database. Can u tell me what is the alternative for the above problem then?

       
      • Juergen Hoeller

        Juergen Hoeller - 2004-04-13

        The message.properties file gets read by Spring's (Reloadable)ResourceBundleMessageSource class, which implements the (very simple) MessageSource interface. As alternative, you can implement your own MessageSource, possibly getting the messages from the database; simply define this implementation as "messageSource" bean in the application context. If you derive from AbstractMessageSource, you can inherit the common adapter code the for the three getMessage methods of the MessageSource interface.

        Juergen

         
    • cheechee

      cheechee - 2004-04-13

      thanks Jholler. It works fine. Only thing, it cannot distinguish between date type & numeric type, etc, unless I give the use the exact field name also to identify. But then, if I do so, I may have to have a message record for each field...and overall i may have a few hundred fields....Is there a better way?

       

Log in to post a comment.

MongoDB Logo MongoDB