【问题标题】:typeMismatch error, even with typeMismatch=msg in messages.properties类型不匹配错误,即使在 messages.properties 中使用类型 Mismatch=message
【发布时间】:2013-05-08 11:25:49
【问题描述】:

当我尝试在我的 jsp 表单的 int 字段中键入 String 时,我仍然遇到致命错误,而不是正常的表单验证。我在messages.properties(Howto validate Collections in Maps)中添加了typeMismatch=msg,但它没有用。我还能错过什么?

【问题讨论】:

  • Spring MVC 应用程序经常使用两个上下文:ApplicationContext 和 Dispatcher Servlet 上下文。检查 messages.properties 在 Dispatcher Servlet 上下文中定义并被他使用。
  • 感谢您的回复。我的 messages.properties 实际上是 messages_fr.properties 并且适用于包括验证消息在内的许多其他消息。

标签: spring spring-mvc


【解决方案1】:

发现问题:你不能这样写:

@RequestMapping(value = "/addSomething", method = RequestMethod.POST)
public String addSomething(
        @ModelAttribute("something") Something something, 
        @Valid Something validSomething,
        BindingResult result,
        ModelMap map) {
    ...

某些属性必须从 String 转换为 int,并且转换是在验证之前进行的。这是不好的!而且validSomething 也没用。这已经解决了问题:

@RequestMapping(value = "/addSomething", method = RequestMethod.POST)
    public String addSomething(
            @Valid @ModelAttribute("something") Something something, 
            BindingResult result,
            ModelMap map) {
        ...

这也证明您最好将代码放在问题中。 “你”是指我。

【讨论】:

    猜你喜欢
    • 2020-06-19
    • 2019-05-24
    • 2013-04-16
    • 1970-01-01
    • 2013-01-16
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多