简体   繁体   English

资源包Spring

[英]Resource Bundle Spring

How can I access the messages from a resource bundle in Spring MVC inside a class that extends from AbstractController ? 如何从一个从AbstractController扩展的类中的Spring MVC中的资源包中访问消息?

I have tried getMessageSourceAccessor().getMessage("a.message"); 我试过getMessageSourceAccessor().getMessage("a.message"); but it it throws this Exception: 但它抛出了这个例外:

org.springframework.web.util.NestedServletException: Request processing failed; org.springframework.web.util.NestedServletException:请求处理失败; nested exception is org.springframework.context.NoSuchMessageException: No message found under code 'a.message' for locale 'en'. 嵌套异常是org.springframework.context.NoSuchMessageException:在代码'a.message'下找不到locale'en'的消息。

When I do the same inside a jsp it works 当我在jsp中执行相同操作时,它可以正常工作

<spring:message code="a.message"/>

You can use one of the getMessage() method variants on the org.springframework.web.servlet.support.RequestContext class. 您可以在org.springframework.web.servlet.support.RequestContext类中使用其中一个getMessage()方法变体。 Something like this works: 像这样的东西有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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