Rakesh Juyal
Rakesh Juyal

Reputation: 36749

Resolve message from property file

How can we resolve a message from properties file? Just like when we use

errors.reject ( "xyz.abc" );

in this case "xyz.abc" is resolved from the property file specified in messageResource ( servlet.xml )

Upvotes: 3

Views: 997

Answers (2)

William Brendel
William Brendel

Reputation: 32189

Implement the MessageSourceAware interface in your class. Something like this should do the trick.

public class Foo implements MessageSourceAware {

    protected MessageSource messageSource;

    ...

    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    ...

    public String bar(HttpServletRequest request, ...) {
        return messageSource.getMessage("xyz.abc", 
                new Object[] { "arg1", "arg2" }, 
                RequestContextUtils.getLocale(request));
    }
}

Upvotes: 7

rodrigoap
rodrigoap

Reputation: 7480

Just implement org.springframework.context.MessageSourceAware

Upvotes: 1

Related Questions