Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

data binding - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...