自定义Spring错误消息 [英] Customize Spring Error Message

查看:146
本文介绍了自定义Spring错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改Spring抛出的默认错误消息。

I want to change the default error messages being thrown by Spring.

我基本上都有这个表单。日期格式应遵循mm / dd / yyyy

I basically have this form. Format of the date should follow "mm/dd/yyyy"

<p>
    <label class="label">Start Date</label>
    <form:input path="dteStartDate" /> 
    <form:errors path="dteStartDate"/>
</p>

一切正常,但在绑定过程中,我会通过以下格式添加此消息:错误标记。

Everything works fine, but during binding I get this message added by the form:error tag.


无法将类型[java.lang.String]的属性值转换为必需类型
[java.util.Date]的属性dteStartDate;嵌套异常是java.lang.IllegalArgumentException:

Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property dteStartDate; nested exception is java.lang.IllegalArgumentException:

无法解析日期:无法解析日期:2010/11/19

Could not parse date: Unparseable date: "2010/11/19"

我可以自定义吗?因此,我可以显示以下内容:

Can I customize this? So that instead of these I could show something like this:


无效的日期格式。格式应为mm / dd / yyyy

Invalid Date Format. Format should be "mm/dd/yyyy"

这是Spring MVC 2.5。

This is Spring MVC 2.5.

推荐答案

您需要定义 MessageSource 以解析邮件的错误代码。错误代码构建为此处描述(所以你将 typeMismatch。< objectName> .dteStartDate )。

You need to define a MessageSource to resolve error code to the message. Error codes are built as described here (so you'll have typeMismatch.<objectName>.dteStartDate).

这篇关于自定义Spring错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆