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

大憨熊 提交于 2020-01-11 11:05:12

问题


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 :

public void validate(Object o) throws ValidationException {

    //Do some logic
    addActionError(getDefaultMessage());
}

回答1:


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));
}


来源:https://stackoverflow.com/questions/28054378/struts-2-how-to-get-i18n-messages-from-within-a-custom-validator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!