【问题标题】:Cannot locate message in MessageSource在 MessageSource 中找不到消息
【发布时间】:2011-07-19 14:34:40
【问题描述】:

我正在尝试运行一个简单的 Spring 应用程序,但出现以下异常:

javax.servlet.ServletException:javax.servlet.jsp.JspTagException:否 在语言环境“en_US”的代码“label.firstname”下找到消息。

我的contact.jsp文件:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ page isELIgnored="false" %>

<html>
<head>
    <title>Spring 3 MVC Series - Contact Manager | viralpatel.net</title>
</head>
<body>

<h2>Contact Manager</h2>

<form:form method="post" action="add.html" commandName="contact">

    <table>
    <tr>
        <td><form:label path="firstname"><spring:message code="label.firstname"/></form:label></td>
        <td><form:input path="firstname" /></td>
    </tr>
    <tr>
        <td><form:label path="lastname"><spring:message code="label.lastname"/></form:label></td>
        <td><form:input path="lastname" /></td>
    </tr>
    <tr>
        <td><form:label path="email"><spring:message code="label.email"/></form:label></td>
        <td><form:input path="email" /></td>
    </tr>
    <tr>
        <td><form:label path="telephone"><spring:message code="label.telephone"/></form:label></td>
        <td><form:input path="telephone" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="<spring:message code="label.addcontact"/>"/>
        </td>
    </tr>
</table>
</form:form>

<h3>Contacts</h3>
<c:if  test="${!empty contactList}">
<table class="data">
<tr>
    <th>Name</th>
    <th>Email</th>
    <th>Telephone</th>
    <th>&nbsp;</th>
</tr>
<c:forEach items="${contactList}" var="contact">
    <tr>
        <td>${contact.lastname}, ${contact.firstname} </td>
        <td>${contact.email}</td>
        <td>${contact.telephone}</td>
        <td><a href="delete/${contact.id}">delete</a></td>
    </tr>
</c:forEach>
</table>
</c:if>

</body>
</html>

我的 spring-servlet.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config />
    <context:component-scan base-package="net.viralpatel.contact" />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

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



    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

<!--    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" -->
<!--        destroy-method="close" p:driverClassName="${jdbc.driverClassName}" -->
<!--        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/ContactManager" p:username="root" p:password="12345" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

我正在点击网址:

localhost:8080/MavenWeb-0.0.1/index

和我的服务器堆栈跟踪:

SEVERE: No message found under code 'label.firstname' for locale 'en_US'.
javax.servlet.jsp.JspTagException: No message found under code 'label.firstname' for locale 'en_US'.
    at org.springframework.web.servlet.tags.MessageTag.doStartTagInternal(MessageTag.java:184)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
    at org.apache.jsp.WEB_002dINF.jsp.contact_jsp._jspx_meth_spring_005fmessage_005f0(contact_jsp.java:252)
    at org.apache.jsp.WEB_002dINF.jsp.contact_jsp._jspx_meth_form_005flabel_005f0(contact_jsp.java:219)
    at org.apache.jsp.WEB_002dINF.jsp.contact_jsp._jspx_meth_form_005fform_005f0(contact_jsp.java:138)
    at org.apache.jsp.WEB_002dINF.jsp.contact_jsp._jspService(contact_jsp.java:91)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:403)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:301)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:162)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

解决问题的步骤:

  1. 仅将 messages_en.properties 文件放在 src/main/resource 文件夹中
  2. 确保文件名正确为 messages_en.properties 而不是 message_en.properties
  3. 当您使用mvn clean install 进行 maven 构建时 messages_en.properties 文件将出现在 目标/项目名称/WEB-INF/classes/messages.properties
  4. 签入 spring-servlet.xml(名称可能不同)并使用 classpath:messages 作为 messageSource 中的基本名称

它应该可以解决我的问题。

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

【问题讨论】:

  • 有什么建议可以解决这个问题吗?

标签: java spring jsp spring-mvc


【解决方案1】:

您的MessageSource bean 定义有点混乱。通常,基本名称是要用于消息解析的文件名的前缀(减去 .properties),Spring 和 JDK ResourceBundle 类会将语言和/或国家/地区缩写附加到该前缀。

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="WEB-INF/message_en.properties/Message" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

例如,如果您打算拥有 messages.propertiesmessages_es.propertiesmessages_de.properties 等资源文件,则应指定基本名称 WEB-INF/messages

您的消息包文件的实际名称是什么?

【讨论】:

  • 我将其修改为 WEB-INF/messages 但仍然遇到同样的问题
  • message_en.properties 是我的消息包文件的实际文件名
  • 如果文件名是message_en.properties(并且它在WEB-INF目录中)那么你应该有一个基本名称WEB-INF/message,而不是WEB-INF/messages。并排除它,您确定该属性存在于文件中吗?此外,我不能 100% 确定解析 WEB-INF 目录中的文件,我通常只是将这些文件放在类路径中(即部署到 WEB-INF/classes 的 Maven 项目中的 src/main/resources)跨度>
  • 这里还有什么需要检查或修复的吗?
  • 1) 文件中是否确实存在该属性? 2) 该文件是否实际存在于 WEB-INF 中? 3) 如果文件在 WEB-INF/classes 中并且基本名称是 messageclasspath:message,你会得到不同的行为吗?等
【解决方案2】:

我遇到了同样的问题,解决方法是:

  1. messages.properties 文件放在源文件夹 src/main/resources

  2. 如下定义 Bean:

    <bean id="messageSource"
             class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
    

你看我在property中放入了与messages.properties文件同名的.properties

这就是问题^^

【讨论】:

    【解决方案3】:

    您必须将“资源”文件夹添加到“Web 部署程序集”。之后将“classpath:messages”替换为“messages”。你需要做的最后一个改变是清理你的项目。

    示例:

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

    这就够了。 :)

    【讨论】:

      【解决方案4】:

      一个重要的考虑是,为了自动重新加载,属性文件不应该在类路径中。服务器通常会缓存属性,所以如果它在类文件夹中,它并不是真的有效。根据 Spring 文档,在 classpath:messages 的情况下,-1 以外的 cachedSeconds 不会有任何影响。

      【讨论】:

        【解决方案5】:

        迟到总比没有好。我有同样的问题。这是我解决它的方法。 我的文件名是:

        messages_fr.properties

        对于法国,因为它是法国的 Spring Tutotrial。但是,我住在荷兰。所以我尝试将其重命名为

        messages_nl.properties

        它有效。不确定这是否是一个长期的解决方法,但我们会看到。

        【讨论】:

          【解决方案6】:

          致任何将面临该问题的人。

          除了所有答案之外,请确保让 Spring Framework 处理 .jsp 文件。如果直接访问 .jsp ,则不会处理 spring 标记,您将看到完全相同的错误。简单地说,通过控制器公开 .jsps

          【讨论】:

            【解决方案7】:

            我遇到了同样的问题,但我只是将其保留为 messages_properties 并解决了问题

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-09-05
              • 1970-01-01
              • 2015-02-09
              • 2021-12-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多