我们将会创建两个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
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
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
>
<%@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
>
<%@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
>