Spring3MVC:Internationlization&Localization Tutorial with Example

我们将会创建两个properties文件,他们包含显示的所有信息,这些文件被放在叫做resources的目录中,

Right click on Project name > New > Source Folder and name it resources.

创建两个文件:messages_en.properties和messages_de.properties在这个目录中,拷贝下面的代码

File: resources/messages_en.properties

label.firstname=First Name
label.lastname=Last Name
label.email=Email
label.telephone=Telephone
label.addcontact=Add Contact
 
label.menu=Menu
label.title=Contact Manager
 
 
label.footer=© dufeng
File: resources/messages_de.properties

label.firstname=Vorname
label.lastname=Familiename
label.email=Email
label.telephone=Telefon
label.addcontact=Addieren Kontakt
 
label.title=Kontakt Manager
label.menu=Menü
 
 
label.footer=© dufeng

添加下面的代码到spring-servlet.xml文件中

File:WebContent/WEB-INF/spring-servlet.xml

< bean id = "messageSource"
     class = "org.springframework.context.support.ReloadableResourceBundleMessageSource" >
     < property name = "basename" value = "classpath:messages" />
     < property name = "defaultEncoding" value = "UTF-8" />
</ bean >
 
< bean id = "localeChangeInterceptor"
     class = "org.springframework.web.servlet.i18n.LocaleChangeInterceptor" >
     < property name = "paramName" value = "lang" />
</ bean >
 
< bean id = "localeResolver"
     class = "org.springframework.web.servlet.i18n.CookieLocaleResolver" >
     < property name = "defaultLocale" value = "en" />
</ bean >
 
< bean id = "handlerMapping"
     class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
     < property name = "interceptors" >
         < ref bean = "localeChangeInterceptor" />
     </ property >
</ bean >
File:WebContent/WEB-INF/jsp/header.jsp

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
 
< h3 >< spring:message code = "label.title" /></ h3 >
 
< span style = "float: right" >
     < a href = "?lang=en" >en</ a >
     |
     < a href = "?lang=de" >de</ a >
</ span >
File:WebContent/WEB-INF/jsp/menu.jsp

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<p><spring:message code="label.menu"/></p>

File:WebContent/WEB-INF/jsp/footer.jsp

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<spring:message code="label.footer"/>

File:WebContent/WEB-INF/jsp/contact.jsp

taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
< html >
< head >
     < title >Spring 3 MVC Series - Contact Manager</ title >
</ head >
< body >
 
 
 
< form:form method = "post" action = "addContact.html" >
 
     < 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 = "lastname" >< spring:message code = "label.email" /></ form:label ></ td >
         < td >< form:input path = "email" /></ td >
     </ tr >
     < tr >
         < td >< form:label path = "lastname" >< 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 >
</ body >
</ html >
程序运行ok!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值