在Wicket中定义自己的反馈消息

时间:2011-04-21 04:13:22

标签: wicket

如何在Wicket中定义自己的反馈消息? 例如:如果我提供的用户名不正确,我想收到一条错误消息,例如“用户名不正确,请尝试重新登录”。而不是使用默认的错误消息。

一个例子是什么样的?

4 个答案:

答案 0 :(得分:27)

您可以使用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));

<强> Index.properties

Required=Provide a ${label} or else...

所有必填字段

myform.name.Required=You have to provide a name

name中的字段myform,当需要时。

password.Required=You have to provide a password

需要时名称为password的任何字段。

phonenumber.Required=A telephone number is obligatory.

需要时名称为phonenumber的任何字段。

这显示了为特定组件设置反馈消息的各种方法。

您还可以将属性文件放在以下组件级别旁边(按重要性顺序排列最高):

  • Page Class
  • 组件类
  • 您的申请类
  • Wickets Application Class

希望有所帮助

答案 1 :(得分:2)

@ user1090145:我在Validator的类中使用了重载的Component error()方法:

private void error(IValidatable<String> validatable, String errorKey) {
    ValidationError error = new ValidationError();
    error.addMessageKey(errorKey);
    validatable.error(error);
}

并通过

validate()中调用它
error(validatable, "your-form.field.text-id");

必须在 YourPage.properties

中定义属性 your-form.field.text-id

来源: Create custom validator in WicketForm validation messages

答案 2 :(得分:1)

您应该将反馈消息设置为会话

message = "message";
Session.get().getFeedbackMessages().success(null, message);

答案 3 :(得分:0)

您可以使用匿名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.