类型不匹配消息的弹簧使用 [英] Spring usage of typeMismatch messages
问题描述
我已经在网络和stackoverflow上进行了一些搜索,看看如何处理我的一个屏幕上的以下消息:
无法将属性值
将[java.lang.String]类型转换为必需的
类型[java.lang.Long] for property
'qtyToShip';嵌套异常是
java.lang.IllegalArgumentException:
无法解析数:Unparseable
number:a
从研究和从网站看,我假设将以下内容添加到我的errors.properties文件将产生所需的结果:
typeMismatch.shippingCommand.qtyToShip = 1。数量要发送的值无效,只接受数字。
typeMismatch.qtyToShip = 2。数量要发送的值无效,只接受数字。
shippingCommand.qtyToShip = 3。数量要发送的值无效,只接受数字。
qtyToShip = 4。数量要发送的值无效,只接受数字。
typeMismatch.java.lang.NumberFormatException = 5。 {0}的值无效,只接受数字。
typeMismatch.java.lang.Integer =必须指定一个整数值。
typeMismatch.java.lang.Long =必须指定整数值。
typeMismatch.java.lang.Float =必须指定一个十进制值。
typeMismatch.java.lang.Double =必须指定一个十进制值。
typeMismatch.int =输入的号码无效
typeMismatch =输入的类型无效
I为消息添加整数值以确定哪一个将显示。
现在我的JSP顶部有以下内容:
< center>
< spring:hasBindErrors name =shippingCommand>
< c:if test =$ {errors.errorCount> 0}>
< h4>以下错误需要更正:< / 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>
以上是我窗体外,我的窗体我有以下(测试理想)所以结果是当我运行我的代码来触发错误时,我看到这个在我的表单内,我收到以下消息:
1。数量要发送的值无效,只接受数字。
哪个来自:typeMismatch.shippingCommand.qtyToShip
表单外部显示:
输入的类型无效
/ pre>
我不明白为什么我可以在我的表单中显示正确的消息,但不在外面?
在控制器中我添加了以下内容:
@Override
protected void initBinder(HttpServletRequest pRequest, ServletRequestDataBinder pBinder)throws异常
{
NumberFormat numberFormat = NumberFormat.getInstance();
pBinder.registerCustomEditor(Long.class,qtyToShip,新的CustomNumberEditor(Long.class,numberFormat,false));
}
感谢
解决方案也许
error.code
不保证返回最具体的消息代码。尝试传递错误消息作为一个整体:< spring:message message =$ {error}/>
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
解决方案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}" />
这篇关于类型不匹配消息的弹簧使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!