2012-11-03 133 views
1

我想在我的spring mvc應用程序中使用國際化。 但我無法得到如何使用它的輸入。 我想要做的水木清華這樣的:Spring MVC國際化

<input id="actionButton" type="submit" value='<spring:message code="LogIn"/>'/> 

但按鈕後,除具有標籤「春天:消息代碼=‘登錄’」,而不是從屬性文件此常數的值。我怎樣才能做到這一點?

+0

您的區域設置攔截器配置是否正確? – Reimeus

+0

是的,如果我使用spring:message作爲輸入值,那麼一切正常。 –

+0

您是否考慮將spring:message內容存儲到var中,然後使用EL從$引用該var? – user998692

回答

1

你有沒有考慮將spring:message內容存儲到一個var,然後用$引用該var?

一個非常有用的教程是在這裏:http://springbyexample.org/examples/basic-webapp-internationalization.html

您必須擁有的applicationContext攔截像

<mvc:interceptors> 
     <bean 
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
      <property name="paramName" value="lang" /> 
     </bean> 
</mvc:interceptors> 

您還需要

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

    <bean id="localeResolver" 
     class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> 

我還想補充一點,在你的xml的開始你應該有這樣的東西:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:security="http://www.springframework.org/schema/security" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 

爲了識別諸如mvc的前綴,這是必要的。確保你有它。

我的消息_ *。properties文件位於源文件夾src/main/resources中,不在webapp中,我不知道這是否重要。

+1

是的,這正是我所擁有的:) –