GWT验证i18n

时间:2017-12-16 05:22:15

标签: validation gwt internationalization

我希望以下可能会有效:

@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

1 个答案:

答案 0 :(得分: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 中覆盖UserValidationMessagesResolver与MyCustomValidationMessagesResolver的使用:

<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