关于国际化:Spring MVC i18n 给出JasperException:在代码下未找到消息

Spring MVC i18n gives JasperException : No message found under code

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

org.apache.jasper.JasperException:javax.servlet.ServletException:javax.servlet.jsp.JspTagException:在代码" label.title"下找不到语言环境" en_US"的消息。

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

spring-context.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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">
   
    <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

1
2
3
4
5
label.title=Contact_Manager
label.firstname=First_Name
label.lastname=Last_Name
label.email=Email
label.telephone=Telephone

page.jsp

1
2
3
4
5
6
7
8
<%@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

我想念什么? 提前致谢。


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

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


我在同样的问题上苦苦挣扎,但我意识到message _ {}。properties必须位于类路径中。 因此,您无需提及基本名称属性值" classpath:messages"。 相反,只需提及"消息"。 这是我的完整配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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>

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


1
2
3
4
5
6
7
8
9
  <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.validation.message => locale / validation / messages_zh_CN.properties


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