问题描述
我在网络和 stackoverflow 中进行了一些搜索,以了解如何处理我在其中一个屏幕上收到的以下消息:
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:
无法转换属性值输入 [java.lang.String] 到需要输入 [java.lang.Long] 作为属性'qtyToShip';嵌套异常是java.lang.IllegalArgumentException:无法解析数字:无法解析编号:a"
从研究和网上看,我认为将以下内容添加到我的 errors.properties 文件会产生预期的结果:
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.
现在在我的 JSP 顶部,我有以下内容:
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.
来源于此:typeMismatch.shippingCommand.qtyToShip
Which comes from this: typeMismatch.shippingCommand.qtyToShip
表格外侧显示:
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));
}
谢谢
推荐答案
也许 error.code
不保证返回最具体的消息代码.尝试将错误消息作为一个整体传递:
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}" />
这篇关于typeMismatch 消息的 Spring 用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!