开发者

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:mess开发者_运维技巧age 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


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}" />   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜