Define own feedback messages in Wicket

前端 未结 4 2191
自闭症患者
自闭症患者 2021-02-13 12:25

How do I define my own feedback messages in Wicket? For example: if I give an incorrect username, I want to get an error message like \"The user name in incorrect, try to login

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-13 12:38

    You can use an anonymous IValidationError class and the messageSource.getMessage method to get a custom message from your property file:

    error(new IValidationError() {
        @Override
        public Serializable getErrorMessage(IErrorMessageSource messageSource) {
            //create a list of the arguments that you will use in your message string
            Map vars = new HashMap();
            vars.put("invalidUsername", getInvalidUsernameInput());
    
            //get the message string from the property file
            return messageSource.getMessage("mysettings.invalid_username", vars);
        }
    });
    

    Sample property file:

    mysettings.invalid_username=The user name "${invalidUsername}" is incorrect.
    

提交回复
热议问题