views:

659

answers:

1

How can I access the messages from a resource bundle in Spring MVC inside a class that extends from AbstractController?

I have tried getMessageSourceAccessor().getMessage("a.message"); but it it throws this Exception:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.context.NoSuchMessageException: No message found under code 'a.message' for locale 'en'.

When I do the same inside a jsp it works

<spring:message code="a.message"/>
+1  A: 

You can use one of the getMessage() method variants on the org.springframework.web.servlet.support.RequestContext class. Something like this works:

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    RequestContext ctx = new RequestContext(request);
    String messageFromBundle = ctx.getMessage("a.message");
}
khill