validationannotationsstruts2ognlmessageformat

Using @RequiredStringValidator messageParams in Struts 2


In my message resources file i have following lines:

error.required={0} is required
labels.email=E-mail

To validate e-mail field I am using annotation validators, like this:

@RequiredStringValidator(key="error.required")
public String getEmail() {
    return email;
}

My question is: how can I pass the labels.email resource value to the {0} param to the message using RequiredStringValidator annotation? I was trying with messageParam property but without any success.


Solution

  • Try this

    @RequiredStringValidator(message = "${getText('error.required', new java.lang.String[] {getText('labels.email')})}")
    

    The getText() could be used if your action extends ActionSupport to get the resource messages by the key specified as a parameter. It applied two times in the same expression to build your message. First time without parameter, second with the parameter and used overloaded getText() implementation.