本文介绍了Spring 数据绑定(@modelattribute)优雅地处理解析异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习使用 Spring MVC 的验证注解.到目前为止,一切都进展顺利,但现在我遇到了一个问题,使我停止了前进.当从网页字段返回的字符串被解析为 Long 时抛出异常,但该字符串包含非数字字符.虽然是预期的行为,但提供用户友好的错误消息被证明是一个挑战.

I've begun learning to use Spring MVC's validation annotations.So far, everything has been going well, but now I've come across an issue which has halted me in my tracks.An exception is being thrown when the string returned from a webpage field is being parsed into a Long, but the string contains non numeric characters. Whilst expected behaviour, presenting a user friendly error message is proving to be a challenge.

到目前为止我所拥有的:

What I have so far:

在webpage.jsp中,显示Spring错误:

in webpage.jsp, the Spring error is being displayed by:

<form:errors path="numberField" cssclass="error"></form:errors>

在网页的控制器中,我们有:

In the controller for the webpage, we have:

@RequestMapping(method = RequestMethod.POST)
public String post(Model model, @Valid @ModelAttribute Item item, BindingResult bindingResult) {

//Stuff

return "redirect:" + ITEM_PAGE;
}

Item 类在成员上有验证注释,它们都按预期工作.

Item class has validation annotations on the members and they all work as expected.

jsp上相应字段旁边的bindingResult返回的错误信息是:

The error message return from the bindingResult on the jsp next to the appropriate field is:

Failed to convert property value of type java.lang.String to required type java.lang.Long for property quantity; nested exception is java.lang.NumberFormatException: For input string: "n0t"

正如我上面所说,这并不意外,但我想用类似的内容替换该消息

As I've said above, this is not unexpected, but I would like to replace that message with something like

Please input a number

我想将它保持在最少的额外代码,但如果唯一的方法是创建我自己的验证器,我可能会走这条路.还有其他方法可以处理异常并返回更好的错误消息吗?

I'd like to keep it to minimal amounts of extra code, but if the only way is to create my own validator, I might go down that route. Are there any other ways to handle the exception and return a nicer error message?

谢谢!

抱歉,这不是可运行的代码,但我不能再发布了.

Sorry that this is not runnable code, but I cant post any more.

推荐答案

您需要定义自定义转换错误消息.假设你有一个 MessageSource 在您的 Spring 配置中设置,将这样的内容添加到包含您的翻译消息的属性文件中:

You need to define custom conversion error message. Assuming that you have a MessageSource set-up in your Spring configuration, add something like this to the properties file containing your translation messages:

typeMismatch.java.lang.Integer = Please enter a number

这将覆盖整数转换失败的默认用户不友好消息.同样,您可以为其他数据类型定义转换错误消息,例如:

That will override the default user unfriendly message for failed integer conversions. Similarly you can define conversion error messages for other data types, like:

typeMismatch.java.time.LocalDate = Enter a valid date

您还可以为特定字段而不是一般数据类型定义转换错误消息.有关这方面的更多详细信息,请参阅我的其他 SO 帖子.

You can also define conversion error messages for specific fields, rather than datatypes in general. See my other SO post for more details about this.

如果你没有配置MessageSource,你可以这样做,例如,如果你使用Java配置:

If you don't have MessageSource configured, you can do it for example like this if you use Java configuration:

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("/WEB-INF/messages/messages");
    return messageSource;
}

这篇关于Spring 数据绑定(@modelattribute)优雅地处理解析异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:30