Struts 2 how to get i18n messages from within a custom validator

前端 未结 1 1079
长情又很酷
长情又很酷 2021-01-22 21:22

How is it possible to get the message from key in the custom validator ?! As mentioned in Struts 2 - reusing Custom Expression Validator you can get default message as :

相关标签:
1条回答
  • 2021-01-22 22:11

    Your custom validator should extend ValidatorSupport class, which has a convenient method getMessage(Object object) to get i18n messages set with key parameter.

    So inside validate method instead of calling getDefaultMessage (which simple returns default message) call getMessage which will evaluate key parameter with additional messageParams.

    public void validate(Object o) throws ValidationException {
        //Do some logic
        addActionError(getMessage(o));
    }
    
    0 讨论(0)
提交回复
热议问题