在Wicket中定义自己的反馈消息
我如何在Wicket中定义自己的反馈消息? 例如:如果我提供的用户名不正确,我希望得到一条错误消息,如“用户名不正确,请尝试再次登录”。而不是使用默认的错误消息在Wicket中定义自己的反馈消息,wicket,Wicket,我如何在Wicket中定义自己的反馈消息? 例如:如果我提供的用户名不正确,我希望得到一条错误消息,如“用户名不正确,请尝试再次登录”。而不是使用默认的错误消息 示例是什么样的?您可以使用error()和warn()和info()显示自己的错误消息。如果要显示依赖于验证器或所需标志的错误,可以定义一个与类同名的属性文件,该类包含字段->消息的映射。例如: Index.java Form form = new Form("myform"); form.add(new TextField("name
示例是什么样的?您可以使用
error()
和warn()
和info()
显示自己的错误消息。如果要显示依赖于验证器或所需标志的错误,可以定义一个与类同名的属性文件,该类包含字段->消息的映射。例如:
Index.java
Form form = new Form("myform");
form.add(new TextField("name").setRequired(true));
form.add(new PasswordTextField("password").setRequired(true));
form.add(new TextField("phonenumber").setRequired(true));
索引.属性
Required=Provide a ${label} or else...
所有必填字段
myform.name.Required=You have to provide a name
表单myform
中的字段name
(如果需要)
password.Required=You have to provide a password
phonenumber.Required=A telephone number is obligatory.
当需要时,名称为密码的任何字段
password.Required=You have to provide a password
phonenumber.Required=A telephone number is obligatory.
当需要时,任何名称为phonenumber
的字段
password.Required=You have to provide a password
phonenumber.Required=A telephone number is obligatory.
这显示了为特定组件设置反馈消息的各种方法
您还可以将特性文件放在以下组件级别旁边(按重要性顺序,最高):
- 页面类
- 组件类
- 你的应用程序类
- Wickets应用程序类
希望对您有所帮助@user1090145:我在Validator类中使用了重载组件的error()方法:
private void error(IValidatable<String> validatable, String errorKey) {
ValidationError error = new ValidationError();
error.addMessageKey(errorKey);
validatable.error(error);
}
属性您的表单.field.text id必须在页面.Properties中定义
资料来源:
及
您应该将反馈消息设置为会话
message = "message";
Session.get().getFeedbackMessages().success(null, message);
您可以使用匿名IValidationError类和messageSource.getMessage方法从属性文件中获取自定义消息:
error(new IValidationError() {
@Override
public Serializable getErrorMessage(IErrorMessageSource messageSource) {
//create a list of the arguments that you will use in your message string
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("invalidUsername", getInvalidUsernameInput());
//get the message string from the property file
return messageSource.getMessage("mysettings.invalid_username", vars);
}
});
mysettings.invalid_username=The user name "${invalidUsername}" is incorrect.
它如何处理验证器的消息?您只使用了setRequired方法。