【问题标题】:Spring MVC i18n <spring:message> gives JasperException : No message found under codeSpring MVC i18n <spring:message> 给出 JasperException : No message found under code
【发布时间】:2013-02-17 20:45:23
【问题描述】:

我正在使用 spring mvc 3.1 jars 构建项目,配置 i18n 文件夹后,tomcat 向我抛出以下异常:

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'label.title' for locale 'en_US'。 p>

我尝试将 i18n 文件夹添加到 eclipse(Juno) 中的类路径中,将 messages*.properties 文件放在 WEB-INF/i18n、WEB-INF/classes/i18n、WEB-INF/ 下classes, WEB-INF/lib, WEB-INF/ 但没用。

spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <annotation-driven />
    <resources location="/, classpath:/META-INF/web-resources/"
        mapping="/resources/**" />
    <default-servlet-handler />
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
    <context:component-scan base-package="com.hoe.spring.controller" />
    <interceptors>
        <beans:bean
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
            p:paramName="lang" />

    </interceptors>
    <beans:bean
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
        id="messageSource" p:basenames="WEB-INF/i18n/messages, WEB-INF/i18n/application"
        p:fallbackToSystemLocale="false"  p:fileEncodings="UTF-8"
    p:defaultEncoding="UTF-8" />
    <beans:bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
        id="localeResolver" p:cookieName="locale" />
</beans:beans>

messages.properties

label.title=Contact_Manager
label.firstname=First_Name
label.lastname=Last_Name
label.email=Email
label.telephone=Telephone

page.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<spring:message code=**"label.title"** var="cName"/>


label.addcontact=Add_Contact
label.menu=Menu

我错过了什么?提前致谢。

【问题讨论】:

    标签: spring-mvc internationalization tomcat7


    【解决方案1】:

    我遇到了同样的问题,我意识到messages_{}.properties 必须在类路径中。因此,您无需提及 basename 属性值“classpath:messages”。而只是提及“messages”。这是我的完整配置。

    <bean id="messageSource"
            class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="messages"/>
            <property name="useCodeAsDefaultMessage" value="true"/>
            <property name="defaultEncoding" value="UTF-8" />       
    </bean>
    
    
    <!--  allow localization through cookie and add interceptor to allow changes to locale -->
    
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
    
    <mvc:interceptors>
        <bean id="localeChangeInterceptor"
              class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
         <property name="paramName" value="lang"/>     
         </bean>          
    </mvc:interceptors>
    

    【讨论】:

      【解决方案2】:

      我已将文件放在 resources/i18n 文件夹下。

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

      【讨论】:

        【解决方案3】:

        尝试将文件放在 eclipse 的 resources/i18n 文件夹下,然后使用 p:basenames="classpath:i18n/messages"。

        【讨论】:

        • 我已经尝试了上述所有解决方案,但徒劳无功。
        • 请发布最新的详细信息,例如,您的 tomcat 服务器下属性文件的实际路径、属性文件名和更新的应用程序上下文 xml 文件。
        【解决方案4】:

        您可以尝试将 message.properties 文件名更改为 messages_en_US.properties

        【讨论】:

        • 我在 WEB-INF/i18n/ 文件夹下也有这些文件(messages_en_US.properties、messages_en_IN.properties、messages.properties、application_en_US.properties、application.properties)
        • 您是否尝试过我指定的上述方法。那个对我有用
        • 是的,我已经尝试过您的选择,但我遇到了同样的错误。我觉得这是一个类路径问题,这里我从 Eclipse IDE 运行 tomcat 服务器。我在 Eclipse 中手动设置了类路径(到消息文件夹)。我还需要做什么吗?
        【解决方案5】:
          <bean id="messageSource"     class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basenames">
                                <list>
                                        <value>locale.messages</value>
                                        <value>locale.validation.messages</value>
                                        <value>locale.email.messages</value>
                                </list>
             </property>
           </bean>
        

        并将属性文件放在 locale 文件夹下(与包结构相同) 如果是 maven/gradle 项目,locale 文件夹位于资源文件夹下 locale.validation.message => locale/validation/messages_en_US.properties

        【讨论】:

        • 将messages_en_US.properties 文件放在WEB-INF/classes/locale/validations/messages 目录下。得到同样的错误。
        猜你喜欢
        • 2020-10-11
        • 1970-01-01
        • 2018-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多