当前位置: 首页 > 知识库问答 >
问题:

Spring中未从消息属性文件中获取验证消息

周和安
2023-03-14

我昨天让它工作,然后我做了一些事情,现在我一直试图修复它几个小时,我只是无法让它工作了。

我有一个包含

表格摘录:

<form:form method="post" action="adduserprofile" modelAttribute="bindableUserProfile">
<table>
    <tr>
        <td><form:label path="firstName">Voornaam</form:label></td>
        <td>
            <form:input path="firstName"/>
            <form:errors path="firstName" />
        </td>
    </tr>
    <tr>
        <td><form:label path="lastName">Achternaam</form:label></td>
        <td>
            <form:input path="lastName"/>
            <form:errors path="lastName" />
        </td>
    </tr>

摘自BindableUserProfile:

   @NotNull
@Size(min = 3, max = 40, message="{errors.requiredfield}")
public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@NotNull
@Size(min = 3, max = 40,  message="errors.requiredfield")
public String getLastName() {
    return lastName;
}
    @RequestMapping(value = "/edit/{userProfileId}", method = RequestMethod.GET)
public String createOrUpdate(@PathVariable Long userProfileId, Model model) {
    if (model.containsAttribute("bindableUserProfile")) {
        model.addAttribute("userProfile", model.asMap().get("bindableUserProfile"));
    } else {
        UserProfile profile = userProfileService.findById(userProfileId);
        if (profile != null) {
            model.addAttribute(new BindableUserProfile(profile));
        } else {
            model.addAttribute(new BindableUserProfile());
        }
    }

    model.addAttribute("includeFile", "forms/userprofileform.jsp");
    return "main";
}

@RequestMapping(value = "/adduserprofile", method = RequestMethod.POST)
public String addUserProfile(@Valid BindableUserProfile userProfile, BindingResult result, Model model) {
    if (result.hasErrors()) {
        return createOrUpdate(null, model);
    }

    UserProfile profile = userProfile.asUserProfile();
    userProfileService.addUserProfile(profile);
    return "redirect:/userprofile";
}

摘自应用程序上下文。xml

   <bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages/messages"/>
</bean>

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource">
        <ref bean="messageSource"/>
    </property>
</bean>

在参考资料/消息中,我有两个文件,messages_en。属性和消息。财产。两者都有相同、简单的内容:

errors.requiredfield=This field is required!!!
  • 当我用一个空的名字提交表单时,我可以在控制器方法'addUserProfile()'中看到错误确实被发现了。
  • 当我用空名字提交表单时,信息识别码会显示在字段旁边,也就是说,在姓氏的情况下,文字文本errors.requiredfield或{errors.requiredfield}。
  • 当我将消息属性值更改为Foo时,比Foo显示为错误消息。因此错误机制本身似乎工作正常。
  • 来自application-context.xml的消息Source bean必须是正确的,因为它说当我更改basename时,它找不到属性文件。
  • 空输入不会被NotNull注释捕获。Spring将空输入视为空字符串,而不是空字符串。

因此,似乎找到了属性文件,并正确处理了验证注释,但Spring不明白它必须用属性文件中的消息替换消息键。


共有2个答案

马冯浩
2023-03-14
匿名用户

在过去的1.5天里,我一直在做同样的事情,最终我找到了解决办法

可能听起来有点疯狂,但这是一个有效的解决方案。:)

@Size(min = 1, max = 50, message = "Email size should be between 1 and 50")

现在从验证标签中删除message=“电子邮件大小应该在1到50之间”

完成此操作后,您的注释将如下所示。

@Size(min = 1, max = 50)

现在在控制器端调试提交表单时调用的方法。下面是我的方法,当用户点击提交时接收请求。

public static ModelAndView processCustomerLoginRequest(IUserService userService, LoginForm loginForm, 
        HttpServletRequest request, HttpSession session, BindingResult result, String viewType, Map<String, LoginForm> model)

现在在方法的第一行放置一个调试点,并调试参数“result”。

BindingResult result

在dubugging时,你会在codes数组中找到这样的字符串。

Size.loginForm.loginId

现在在属性文件中定义这个字符串,并根据该字符串定义一条消息。编译并执行。每当该注释无法验证时,就会显示该消息。

Size.loginForm.loginId=email shouldn't be empty.

基本上,spring将自己的字符串作为其属性文件消息的键。在上面的键中:

  • size(@size)=验证注释名称
  • loginForm=我的类名
  • loginId=loginForm类中的属性名。

这种方法的美妙之处在于,在使用Spring国际化时,它也运行良好。随着语言的变化,它会自动切换消息文件。

章琛
2023-03-14

哎呀,我想这根本就不应该起作用。

我认为可以将JSR-303注释的“message”属性解释为键,以便从消息中获取相关的错误消息。属性文件,但我觉得我错了。

@Size(最小值=3,最大值=40,message=“errors.requiredfield”)

我的同事编写了一个层,为我们创造了这种行为,但它在默认情况下不起作用。好像我让它工作过一次,因为我在使用

@Size(最小值=3,最大值=40,message=“{errors.requiredfield}”)

卷曲的大括号导致Spring启动了一个查找和替换过程,该过程使用。属性文件作为源。不过,第二种选择仍然有效。

 类似资料:
  • 我正在创建一个简单的Spring REST应用程序,下面是我的Spring配置类; 这就是我在控制器类中所做的; 我已经在EmployeeModel中注释了我的姓名字段,如下所示; 这是我的messages.properties文件,在src/main/资源文件夹中。 但我无法读取此错误消息,我只收到@NotEmpty的默认消息。 我甚至尝试了以下配置; 请帮我拿这个!

  • 我有一个简单的登录模型类,几乎没有数据注释验证 View是一个部分View,如下所示: 我相信我已经做了验证消息显示所需的所有事情,但它没有,客户端验证也没有。在母版页的头部区域,我还包括了以下脚本 是什么导致了这个问题?解决办法是什么?

  • 我正在使用Spring Boot 2.1.8构建一个项目,我的POM中有spring-boot-starter-web,我可以看到Maven将hibernate-validator 6.0.17拉到类路径上。 我在资源文件夹中有我的消息,它们似乎可以正确地查找,这样当我更改区域设置时,Spring就可以从正确的文件中加载消息。 my@RESTController中的相关方法采用@Valid和@Re

  • 问题内容: 好的,简单的问题,但对我来说很重要。 因此,其他android客户端正在发送此xml消息: 我的听众大致是这样的: 在这种情况下,我想在“ received”元素标签内获取属性“ id”的值。但是我在日志中看到的是这样的: 那么我如何获得“ HVgQw-5”? 更新 其实有些奇怪……我从我的SMACK调试中收到了xmlordinh,如下所示: 但是当我执行message.toXML时,

  • 我的设想是: 我有一个带有JSR303验证的pojo类,如下所示: 我的要求是,我想知道哪个特定字段没有通过验证,我想把消息写在“消息”属性中。 目前,我能够跟踪字段名称,但不能跟踪“消息”属性中存在的消息。 我使用以下方法。 我还希望将消息写在注释的属性“消息”中。我怎样才能得到它?有可能这样做吗?