Data binding 类型不匹配消息的Spring用法

Data binding 类型不匹配消息的Spring用法,data-binding,spring-mvc,Data Binding,Spring Mvc,我在web和stackoverflow中进行了一些搜索,以了解如何处理我在其中一个屏幕上看到的以下消息: 无法转换的属性值 键入[java.lang.String]为必填项 为属性键入[java.lang.Long] “qtyToShip”;嵌套异常是 java.lang.IllegalArgumentException: 无法分析编号:不可解析 编号:“a” 通过研究和查看web,我认为将以下内容添加到我的errors.properties文件将产生预期的结果: typeMismatch.sh

我在web和stackoverflow中进行了一些搜索,以了解如何处理我在其中一个屏幕上看到的以下消息:

无法转换的属性值 键入[java.lang.String]为必填项 为属性键入[java.lang.Long] “qtyToShip”;嵌套异常是 java.lang.IllegalArgumentException: 无法分析编号:不可解析 编号:“a”

通过研究和查看web,我认为将以下内容添加到我的errors.properties文件将产生预期的结果:

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
我将整数值添加到消息中,以确定将显示哪个整数值

现在,在JSP的顶部,我有以下内容:

  <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>
@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));
}
它来自于:typemissmatch.shippingCommand.qtyToShip

表单的外部显示:

Invalid type entered
我不明白的是,为什么我能够在表单内部而不是外部显示正确的消息

在控制器中,我添加了以下内容:

  <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>
@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));
}

谢谢

可能是
错误。code
不能保证返回最具体的消息代码。尝试整体传递错误消息:

<spring:message message = "${error}" />