在 spring MVC 中的语言环境“en_GB”错误的代码“login.userName”下找不到任何消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9511457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:58:47  来源:igfitidea点击:

No message found under code 'login.userName' for locale 'en_GB' error in spring MVC

springspring-mvc

提问by Harry

In my spring-servlet.xml I have

在我的 spring-servlet.xml 我有

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages" />
</bean>

My messages.properties file is inside my WEB-INF/classes and its named as messages_EN.properties which contains like

我的messages.properties 文件在我的WEB-INF/classes 里面,它被命名为messages_EN.properties,其中包含

login.userName=Username.
login.password=pssword.

My jsp file has

我的jsp文件有

 <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
 <%@taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>

 <html> 
<head> 
 <title> Login </title>
 </head> 
 <body>
 <form:form method="post" action="home.htm">
  <label> <spring:message code="login.userName"/></label>
 </form:form>
 </body>
 </html>

when I run this I am getting the below error message

当我运行它时,我收到以下错误消息

  No message found under code 'login.userName' for locale 'en_GB'.

If anyone can help me where I am wrong. Also if I need to change to some other language then what I need to do?

如果有人可以帮助我哪里出错了。另外,如果我需要更改为其他语言,那么我需要做什么?

回答by ndeverge

You have a problem in the location config of your message.properties file.

您的 message.properties 文件的位置配置有问题。

If the file is located under the WEB-INF/classesdirectory, then the Spring config should be :

如果文件位于WEB-INF/classes目录下,则 Spring 配置应为:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="WEB-INF/classes/messages" />
</bean>

And the name of the file should be either :

文件名应该是:

  • messages.properties
  • messages_en.properties
  • messages_en_GB.properties
  • 消息.属性
  • messages_en.properties
  • messages_en_GB.properties

回答by Jana

Resource Mapping to copy Messages.properties file from src -> main -> resources -> Messages.properties file

资源映射从 src -> main -> resources -> Messages.properties 文件复制 Messages.properties 文件

 <mvc:resources location="/resources/" mapping="/resources/**" />

 <bean id="messageSource"  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
 <property name="basename" value="classpath:Messages" />