简体   繁体   中英

Resource Bundle Spring

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"/>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM