No message found under code 'login.userName

2020-07-20 04:08发布

问题:

In my spring-servlet.xml I have

<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

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

My jsp file has

 <%@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?

回答1:

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

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

<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


回答2:

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

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

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