在系列(7)中我们讲了数据的格式化显示,spring在做格式化展示的时候已经做了国际化处理,那么如何将我们网站的其它内容(如菜单、标题等)做国际化处理呢?这就是本篇要将的内容—>国际化。
一.基于浏览器请求的国际化实现:
首先配置我们项目的springservlet-config.xml文件添加的内容如下:
1
|
2
3
4
5
6
|
<bean id= "messagesource" class = "org.springframework.context.support.resourcebundlemessagesource" >
<!-- 国际化信息所在的文件名 -->
<property name= "basename" value= "messages" />
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name= "usecodeasdefaultmessage" value= "true" />
</bean>
|
在com.demo.web.controllers包中添加globalcontroller.java内容如下:
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
35
36
|
package com.demo.web.controllers;
import java.util.date;
import javax.servlet.http.httpservletrequest;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.servlet.support.requestcontext;
import com.demo.web.models.formatmodel;
@controller
@requestmapping (value = "/global" )
public class globalcontroller {
@requestmapping (value= "/test" , method = {requestmethod.get})
public string test(httpservletrequest request,model model){
if (!model.containsattribute( "contentmodel" )){
//从后台代码获取国际化信息
requestcontext requestcontext = new requestcontext(request);
model.addattribute( "money" , requestcontext.getmessage( "money" ));
model.addattribute( "date" , requestcontext.getmessage( "date" ));
formatmodel formatmodel= new formatmodel();
formatmodel.setmoney( 12345.678 );
formatmodel.setdate( new date());
model.addattribute( "contentmodel" , formatmodel);
}
return "globaltest" ;
}
}
|
这里展示模型还用系列(7)中的作为演示。
在项目中的源文件夹resources中添加messages.properties、messages_zh_cn.properties、messages_en_us.properties三个文件,其中messages.properties、messages_zh_cn.properties里面的"money", "date",为中文,messages_en_us.properties里面的为英文。
在views文件夹中添加globaltest.jsp视图,内容如下:
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
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8"
pageencoding= "utf-8" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" " http://www.w3.org/tr/html4/loose.dtd " >
<% @taglib prefix= "spring" uri= " http://www.springframework.org/tags " %>
<html>
<head>
<meta http-equiv= "content-type" content= "text/html; charset=utf-8" >
<title>insert title here</title>
</head>
<body>
下面展示的是后台获取的国际化信息:<br/>
${money}<br/>
${date}<br/>
下面展示的是视图中直接绑定的国际化信息:<br/>
<spring:message code= "money" />:<br/>
<spring:eval expression= "contentmodel.money" ></spring:eval><br/>
<spring:message code= "date" />:<br/>
<spring:eval expression= "contentmodel.date" ></spring:eval><br/>
</body>
</html>
|
运行测试:
更改浏览器语言顺序,刷新页面:
二.基于session的国际化实现:
在项目的springservlet-config.xml文件添加的内容如下(第一种时添加的内容要保留):
1
|
2
3
4
5
6
|
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(请求/session/cookie)则必需配置 -->
<bean class = "org.springframework.web.servlet.i18n.localechangeinterceptor" />
</mvc:interceptors>
<bean id= "localeresolver" class = "org.springframework.web.servlet.i18n.sessionlocaleresolver" />
|
更改globaltest.jsp视图为如下内容:
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
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8"
pageencoding= "utf-8" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" " http://www.w3.org/tr/html4/loose.dtd " >
<% @taglib prefix= "spring" uri= " http://www.springframework.org/tags " %>
<html>
<head>
<meta http-equiv= "content-type" content= "text/html; charset=utf-8" >
<title>insert title here</title>
</head>
<body>
<a href= "test?langtype=zh" rel= "external nofollow" >中文</a> | <a href= "test?langtype=en" rel= "external nofollow" >英文</a><br/>
下面展示的是后台获取的国际化信息:<br/>
${money}<br/>
${date}<br/>
下面展示的是视图中直接绑定的国际化信息:<br/>
<spring:message code= "money" />:<br/>
<spring:eval expression= "contentmodel.money" ></spring:eval><br/>
<spring:message code= "date" />:<br/>
<spring:eval expression= "contentmodel.date" ></spring:eval><br/>
</body>
</html>
|
更改globalcontroller.java为如下内容:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package com.demo.web.controllers;
import java.util.date;
import java.util.locale;
import javax.servlet.http.httpservletrequest;
import org.springframework.context.i18n.localecontextholder;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.servlet.i18n.sessionlocaleresolver;
import org.springframework.web.servlet.support.requestcontext;
import com.demo.web.models.formatmodel;
@controller
@requestmapping (value = "/global" )
public class globalcontroller {
@requestmapping (value= "/test" , method = {requestmethod.get})
public string test(httpservletrequest request,model model, @requestparam (value= "langtype" , defaultvalue= "zh" ) string langtype){
if (!model.containsattribute( "contentmodel" )){
if (langtype.equals( "zh" )){
locale locale = new locale( "zh" , "cn" );
request.getsession().setattribute(sessionlocaleresolver.locale_session_attribute_name,locale);
}
else if (langtype.equals( "en" )){
locale locale = new locale( "en" , "us" );
request.getsession().setattribute(sessionlocaleresolver.locale_session_attribute_name,locale);
}
else
request.getsession().setattribute(sessionlocaleresolver.locale_session_attribute_name,localecontextholder.getlocale());
//从后台代码获取国际化信息
requestcontext requestcontext = new requestcontext(request);
model.addattribute( "money" , requestcontext.getmessage( "money" ));
model.addattribute( "date" , requestcontext.getmessage( "date" ));
formatmodel formatmodel= new formatmodel();
formatmodel.setmoney( 12345.678 );
formatmodel.setdate( new date());
model.addattribute( "contentmodel" , formatmodel);
}
return "globaltest" ;
}
}
|
运行测试:
三.基于cookie的国际化实现:
把实现第二种方法时在项目的springservlet-config.xml文件中添加的
<bean id="localeresolver" class="org.springframework.web.servlet.i18n.sessionlocaleresolver" />
注释掉,并添加以下内容:
<bean id="localeresolver" class="org.springframework.web.servlet.i18n.cookielocaleresolver" />
更改globalcontroller.java为如下内容:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package com.demo.web.controllers;
import java.util.date;
import java.util.locale;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.springframework.context.i18n.localecontextholder;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.servlet.i18n.cookielocaleresolver;
//import org.springframework.web.servlet.i18n.sessionlocaleresolver;
import org.springframework.web.servlet.support.requestcontext;
import com.demo.web.models.formatmodel;
@controller
@requestmapping (value = "/global" )
public class globalcontroller {
@requestmapping (value= "/test" , method = {requestmethod.get})
public string test(httpservletrequest request, httpservletresponse response, model model, @requestparam (value= "langtype" , defaultvalue= "zh" ) string langtype){
if (!model.containsattribute( "contentmodel" )){
/*if(langtype.equals("zh")){
locale locale = new locale("zh", "cn");
request.getsession().setattribute(sessionlocaleresolver.locale_session_attribute_name,locale);
}
else if(langtype.equals("en")){
locale locale = new locale("en", "us");
request.getsession().setattribute(sessionlocaleresolver.locale_session_attribute_name,locale);
}
else
request.getsession().setattribute(sessionlocaleresolver.locale_session_attribute_name,localecontextholder.getlocale());*/
if (langtype.equals( "zh" )){
locale locale = new locale( "zh" , "cn" );
//request.getsession().setattribute(sessionlocaleresolver.locale_session_attribute_name,locale);
( new cookielocaleresolver()).setlocale (request, response, locale);
}
else if (langtype.equals( "en" )){
locale locale = new locale( "en" , "us" );
//request.getsession().setattribute(sessionlocaleresolver.locale_session_attribute_name,locale);
( new cookielocaleresolver()).setlocale (request, response, locale);
}
else
//request.getsession().setattribute(sessionlocaleresolver.locale_session_attribute_name,localecontextholder.getlocale());
( new cookielocaleresolver()).setlocale (request, response, localecontextholder.getlocale());
//从后台代码获取国际化信息
requestcontext requestcontext = new requestcontext(request);
model.addattribute( "money" , requestcontext.getmessage( "money" ));
model.addattribute( "date" , requestcontext.getmessage( "date" ));
formatmodel formatmodel= new formatmodel();
formatmodel.setmoney( 12345.678 );
formatmodel.setdate( new date());
model.addattribute( "contentmodel" , formatmodel);
}
return "globaltest" ;
}
}
|
运行测试:
关于<bean id="localeresolver" class="org.springframework.web.servlet.i18n.cookielocaleresolver" />3个属性的说明(可以都不设置而用其默认值):
1
|
2
3
4
5
6
7
8
|
<bean id= "localeresolver" class = "org.springframework.web.servlet.i18n.cookielocaleresolver" >
<!-- 设置cookiename名称,可以根据名称通过js来修改设置,也可以像上面演示的那样修改设置,默认的名称为 类名+locale(即:org.springframework.web.servlet.i18n.cookielocaleresolver.locale-->
<property name= "cookiename" value= "lang" />
<!-- 设置最大有效时间,如果是- 1 ,则不存储,浏览器关闭后即失效,默认为integer.max_int-->
<property name= "cookiemaxage" value= "100000" >
<!-- 设置cookie可见的地址,默认是“/”即对网站所有地址都是可见的,如果设为其它地址,则只有该地址或其后的地址才可见-->
<property name= "cookiepath" value= "/" >
</bean>
|
四.基于url请求的国际化的实现:
首先添加一个类,内容如下:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.util.locale;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.springframework.web.servlet.dispatcherservlet;
import org.springframework.web.servlet.localeresolver;
public class myacceptheaderlocaleresolver extends acceptheaderlocaleresolver {
private locale mylocal;
public locale resolvelocale(httpservletrequest request) {
return mylocal;
}
public void setlocale(httpservletrequest request, httpservletresponse response, locale locale) {
mylocal = locale;
}
}
|
然后把实现第二种方法时在项目的springservlet-config.xml文件中添加的
<bean id="localeresolver" class="org.springframework.web.servlet.i18n.sessionlocaleresolver" />
注释掉,并添加以下内容:
1
|
|
<bean id= "localeresolver" class = "xx.xxx.xxx.myacceptheaderlocaleresolver" />
|
“xx.xxx.xxx”是刚才添加的myacceptheaderlocaleresolver 类所在的包名。
保存之后就可以在请求的url后附上 locale=zh_cn 或 locale=en_us 如 http://xxxxxxxx?locale=zh_cn 来改变语言了,具体这里不再做演示了。
国际化部分的内容到此结束。
代码下载:springmvci18n.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/liukemng/p/3750117.html