编程语言
首页 > 编程语言> > java – spring / hibernate JSR-303(Bean验证)本地化消息中的命名参数

java – spring / hibernate JSR-303(Bean验证)本地化消息中的命名参数

作者:互联网

我有一个简单的bean定义注册表单模型上的用户凭据:

public class UserCreds {
    @Length(min=1, max=50)
    @NotEmpty
    private String username;

    @Email
    private String email;

    @NotEmpty
    @Length(min=1, max=50)
    private String password;

    private String passwordConfirm;

    ... only obvious getters and setters below this line ...
}

我在“userCreds”模型属性下传递它的实例,所以我有一个以下本地化包:

NotEmpty.userCreds.username=Username is required
NotEmpty.userCreds.password=Passsword is required
Length.userCreds.username=Username length must be between {2} and {1} symbols
Length.userCreds.password=Password length must be between {2} and {1} symbols
Email.userCreds.email=Ill-formed e-mail address

它的工作原理!

这里的问题是:我不想使用这个神秘的{N}占位符,它们可能会偶尔改变它们的位置(在’max’之上的捆绑绑定到{1}并且’min’绑定到{2},这是在最不明显的定位)我希望使用注释的全名,如

Length.userCreds.username=Username length must be between {min} and {max} symbols

(这是行不通的 – 当我尝试它 – 我得到java.lang.IllegalArgumentException:无法解析参数数min最小异常)

正如我在代码中定义消息时所做的那样(以下示例也有效):

@Length(min=1, max=50, message = "Username length must be between {min} and {max} symbols")
@NotEmpty
private String username;

但是当我在代码中定义它时,我失去了为不同语言本地化消息的能力.

做什么?

更新1

在我的IDE中调试调试器几个小时后,我发现使用以下类:

public class BakaMessageSource extends ReloadableResourceBundleMessageSource {

    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        /* original implementation *
        for (String basename : basenames) {
            List<String> filenames = calculateAllFilenames(basename, locale);
            for (String filename : filenames) {
                PropertiesHolder propHolder = getProperties(filename);
                MessageFormat result = propHolder.getMessageFormat(code, locale);
                if (result != null) {
                    return result;
                }
            }
        }
        */
        return null;
    }
}

使用以下spring-servlet.xml声明:

<bean id="messageSource" class="org.eientei.radiorandom.framework.crutchy.BakaMessageSource"
          p:defaultEncoding="UTF-8"
          p:alwaysUseMessageFormat="false"
            >
        <property name="basenames">
            <list>
                <!-- my application-specific localization bundles -->
                <value>classpath:messages/root/errorMessages</value>
                <value>classpath:messages/root/langMessages</value>
                <value>classpath:messages/user/signupMessages</value>
            </list>
        </property>
    </bean>

而不是原始的ReloadableResourceBundleMessageSource实际上有助于获取具有命名参数的本地化消息.

但我想在那时交换类不是一个合适的解决方案.会是哪一个?

更新2

我是个白痴.我忘记了代码中的消息,并认为它已被正确插值.吉兹.
所以,按字母排序…猜我不能要求更多.

解决方法:

我认为从Spring 3.0.4开始,实际的顺序是按字母顺序排列的(“max”小于“min”).

来自SPR-6730讨论(Juergen Hoeller帖子)

For the first option, Spring-managed field error arguments include the
actual constraint annotation attributes in alphabetical order now. So
for @Size(min=1,max=5), it’ll include the field name as {0} , the max
value as {1} , and the min value as {2}

https://jira.springsource.org/browse/SPR-6730

标签:hibernate-validator,java,spring,localization,bean-validation
来源: https://codeday.me/bug/20190901/1783521.html