从控制器获取 messageBundles 时出错
我正在尝试从控制器获取消息区域设置,但收到此错误:
org.springframework.web.util.NestedServletException
:请求处理失败;嵌套异常为org.springframework.context.NoSuchMessageException
:在语言环境'es_ES'
的代码'error.alias'
下找不到消息。
在我的控制器中:
ApplicationContext context = MyContext.getApplicationContext();
List errors = null;
// Validar parametros
if (!Filter.onlyLetters(alias)) {
errors.add(context.getMessage("error.alias", null, Locale.getDefault()));
}
MyContext.getApplicationContext()
从实现 ApplicationContextAware
的 MyApplicationContextAware
获取 spring 上下文。
public class MyContext {
private static ApplicationContext context;
public static void setApplicationContext(ApplicationContext applicationContext) {
context = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return context;
}
}
public class MyApplicationContextAware implements ApplicationContextAware {
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
MyContext.setApplicationContext(ctx);
}
}
<bean id="myApplicationContextAware" class="...MyApplicationContextAware" />
“error.alias”存在于 messages.properties 和 messages_es_ES.properties 中。
/WEB-INF/classes/messages.properties
/WEB-INF/classes/messages_es_ES.properties
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
I am trying get a message locale from Controller but I obtain this error:
org.springframework.web.util.NestedServletException
: Request processing failed; nested exception isorg.springframework.context.NoSuchMessageException
: No message found under code'error.alias'
for locale'es_ES'
.
In my Controller:
ApplicationContext context = MyContext.getApplicationContext();
List errors = null;
// Validar parametros
if (!Filter.onlyLetters(alias)) {
errors.add(context.getMessage("error.alias", null, Locale.getDefault()));
}
MyContext.getApplicationContext()
obtain spring context from MyApplicationContextAware
that implements ApplicationContextAware
.
public class MyContext {
private static ApplicationContext context;
public static void setApplicationContext(ApplicationContext applicationContext) {
context = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return context;
}
}
public class MyApplicationContextAware implements ApplicationContextAware {
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
MyContext.setApplicationContext(ctx);
}
}
<bean id="myApplicationContextAware" class="...MyApplicationContextAware" />
"error.alias" exist in messages.properties and messages_es_ES.properties.
/WEB-INF/classes/messages.properties
/WEB-INF/classes/messages_es_ES.properties
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将完整路径设置为基本名称:
编辑:运行 Web 应用程序或单元测试期间是否会发生这种情况?我猜这一定是类路径问题,请检查“WEB-INF/classes”在您尝试执行期间是否位于类路径上。
Try setting the full path as basename:
EDIT: Does this happen whilst running your web application or during unit tests? I'm guessing it must a classpath issue, check if 'WEB-INF/classes' is on the classpath during the execution you are attempting.