Announcement Announcement Module
Collapse
No announcement yet.
CustomNumberEditor: how to overrule error messages Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • CustomNumberEditor: how to overrule error messages

    Hi all,

    I have registered a CustomNumberEditor in my Controller that enables me to have optional Integer properties.

    Code:
    protected void initBinder(HttpServletRequest request,
    	ServletRequestDataBinder binder) throws Exception {
    
    	PropertyEditor editor = new CustomNumberEditor(Integer.class, true);
    	binder.registerCustomEditor(Integer.class, "year", editor);
    }
    Everything works as expected, but when I type an invalid value in the HTML form, I get the following error message shown in my form:

    Code:
    Failed to convert property value of type [java.lang.String] to required type [java.lang.Integer] for property year; nested exception is java.lang.NumberFormatException: For input string: "invalid"
    Is there a way that I can override this message and have a error message from my messages.properties (I have this configured in a MessageSource bean) file shown?

    Thanks in advance,

    Arjan Huijzer

  • #2
    You need to set up an application resource message bundle in your applicationContext.xml:
    Code:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
      <property name="basenames">
        <list>
           <value>path/to/errors</value>
        </list>
      </property>
    </bean>
    Then in your file named path.to.errors.properties, place the following key for each type of bind class that you want a custom message for:

    typeMismatch.java.lang.Integer=My tidy error message for integers
    typeMismatch.java.lang.Double=My tidy error message for doubles

    etc...

    cheers, toby

    Comment


    • #3
      That was exactly what I was looking for. Worked perfectly!

      Comment


      • #4
        something more

        hi,
        i have also done this. but i want more, as my page is containing more the 5 number fields.And i want to show the message with proptery name containing the number format error.

        Regards,
        Shahzad

        Comment


        • #5
          typeMismatch.java.lang.Integer={0} should be integer
          ?
          If this is not enough, this javadoc might help:
          http://www.springframework.org/docs/...sResolver.html

          Comment


          • #6
            I have implemented this solution and the "{0}" part gets correctly filled with the name of the field that caused the error. The next question I have is this: how can I get Spring to use a user defined label to replace the "{0}" instead of the field name?

            Thanks in advance for any help,

            Arjan Huijzer

            Comment


            • #7
              in the same javadoc there is a description of message resolution for property names. The simplest is
              someField = the description of the filed
              or
              outerObject.someFiled = the description of the filed
              Also, don't forget, that message resolution properly works inside corresponding <spring:bind>, when you are operationg with Error object

              Comment

              Working...
              X