GWT验证i18n

问题描述 投票:0回答:1

我希望以下可能有效:

@Size(min = 1, message = "{my.custom.message}")
private String name;

使用我的源路径中的ValidationMessages.properties,与我的其他文本资源一起使用:

my.custom.message=it is kinda short

......但约束违规仍然是{my.custom.message}

是否必须执行一些特殊操作才能获取ValidationMessages.properties文件?请记录在哪里?

显然它应该工作,不知何故:https://github.com/gwtproject/gwt/issues/5762

validation gwt internationalization
1个回答
1
投票

感谢TBroyer's reply,我设法让它在我的项目中工作。

简而言之...

...定义MyCustomValidationMessages接口:

public interface MyCustomValidationMessages extends com.google.gwt.i18n.client.ConstantsWithLookup {
    @DefaultStringValue("NotNull constraint violated")
    @Key("notNull")
    String notNull();

    @DefaultStringValue("Size constraint violated")
    @Key("size")
    String size();
}

...在它旁边创建MyCustomValidationMessages.properties:

notNull=must not be null
size=must be at least {min} characters long

...定义MyCustomValidationMessagesResolver类并使其使用来自MyCustomValidationMessages接口的消息:

public class MyCustomValidationMessagesResolver extends AbstractValidationMessageResolver
        implements UserValidationMessagesResolver {
    public MyCustomValidationMessagesResolver() {
        super(GWT.create(MyCustomValidationMessages.class));
    }
}

...在您的AmazingModule.gwt.xml中,使用MyCustomValidationMessagesResolver覆盖UserValidationMessagesResolver:

<replace-with class="amazing.project.client.MyCustomValidationMessagesResolver">
    <when-type-is class="com.google.gwt.validation.client.UserValidationMessagesResolver"/>
</replace-with>

...在DTO bean中应用约束(注意大括号):

public class MyDto {
    @NotNull(message = "{notNull}")
    @Size(min = 1, message = "{size}")
    private String name;
}

瞧!

Working example is here.

如果您确定已完成所有操作并且它无法正常工作,请尝试删除GWT的临时文件夹并重新启动IDE。有时仅仅重建项目并重新启动代码服务器(在Idea中)可能还不够。 #PITA

© www.soinside.com 2019 - 2024. All rights reserved.