资源包春天 [英] Resource Bundle Spring
问题描述
如何从一个扩展自 AbstractController
的类中的Spring MVC中的资源包中访问消息?
How can I access the messages from a resource bundle in Spring MVC inside a class that extends from AbstractController
?
我已经尝试了 getMessageSourceAccessor()。getMessage(a.message);
但它抛出此异常:
I have tried getMessageSourceAccessor().getMessage("a.message");
but it it throws this Exception:
org.springframework.web.util.NestedServletException:
请求处理失败;嵌套的
异常是
org.springframework.context.NoSuchMessageException:
在代码
'a.message'下找不到locale'en'的消息。
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'.
当我在jsp中做同样的事情时,它可以工作
When I do the same inside a jsp it works
<spring:message code="a.message"/>
推荐答案
您可以使用其中一个getMessage()方法变体在org.springframework.web.servlet.support.RequestContext类中。这样的工作:
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");
}
这篇关于资源包春天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!