Resource Bundle Spring

╄→гoц情女王★ 提交于 2020-01-02 03:27:05

问题


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:


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


来源:https://stackoverflow.com/questions/1694974/resource-bundle-spring

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