Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring usage of typeMismatch messages

I have done some searches on the web and in stackoverflow to see how to handle the following message that I get on one of my screens:

Failed to convert property value of type [java.lang.String] to required type [java.lang.Long] for property 'qtyToShip'; nested exception is java.lang.IllegalArgumentException: Could not parse number: Unparseable number: "a"

From research and from looking at the web I assumed that adding the following to my errors.properties file would produce the desired results:

typeMismatch.shippingCommand.qtyToShip=1. Invalid value for Qty To Ship, accepts only numbers.  
typeMismatch.qtyToShip=2. Invalid value for Qty To Ship, accepts only numbers. 
shippingCommand.qtyToShip=3. Invalid value for Qty To Ship, accepts only numbers. 
qtyToShip=4. Invalid value for Qty To Ship, accepts only numbers.


typeMismatch.java.lang.NumberFormatException=5. Invalid value for {0}, accepts only numbers. 
typeMismatch.java.lang.Integer=Must specify an integer value. 
typeMismatch.java.lang.Long=Must specify an integer value. 
typeMismatch.java.lang.Float=Must specify a decimal value. 
typeMismatch.java.lang.Double=Must specify a decimal value.  
typeMismatch.int=Invalid number entered 
typeMismatch=Invalid type entered

I add integer values to the message to determine which one would show up.

Now at the top of my JSP I have the following:

  <center>
    <spring:hasBindErrors name="shippingCommand">
      <c:if test="${errors.errorCount > 0 }">
        <h4>Following errors need to be corrected:</h4>
        <font color="red">
          <c:forEach items="${errors.allErrors}" var="error">
            <spring:message code="${error.code}" arguments="${err.arguments}" text="${error.defaultMessage}"/><br />
          </c:forEach>
        </font>
      </c:if>
    </spring:hasBindErrors>
  </center>

The above is outside my Form, inside my Form I have the following (testing out ideals)

So the results is that when I run my code to trigger the error, I see that inside my form I get the following message:

1. Invalid value for Qty To Ship, accepts only numbers.

Which comes from this: typeMismatch.shippingCommand.qtyToShip

The outside of the form displayes:

Invalid type entered

What I do not understand is why am I able to display the correct message inside my form but not outside?

In the controller I have added the following:

@Override
protected void initBinder(HttpServletRequest pRequest, ServletRequestDataBinder pBinder) throws Exception
{
    NumberFormat numberFormat = NumberFormat.getInstance();
    pBinder.registerCustomEditor(Long.class, "qtyToShip", new CustomNumberEditor(Long.class, numberFormat, false));
}

Thanks

like image 635
boyd4715 Avatar asked Jan 31 '11 17:01

boyd4715


1 Answers

Perhaps error.code doesn't guarantee to return the most specific message code. Try to pass error message as a whole:

<spring:message message = "${error}" />   
like image 181
axtavt Avatar answered Jan 02 '23 20:01

axtavt