1. In path/src folder, create ApplicationResources.properties and ApplicationResources_zh_CN.properties. The resource file name should be ended with .properties, and different language resouce files should be end with like _zh_CN. The chinese resouce file should be changed to Unicode. We can use command native2ascii (which is a java command) to help us. But first we need to create a file ApplicationResources_zh.properties which contains Chinese characters. The command is: native2ascii -encoding GB2312 ApplicationResources_zh.properties ApplicationResources_zh_CN.properties;
2. Confirm that the <message-resource> attribute is configured correctly in struts-config.xml;
3. Using <bean:message key="xxx" /> in .jsp files.
A example:
1. create resoucres files.
Create ApplicationResources.properties in Eclipse.
--------------------------------------------------
ApplicationResources.properties
#login resources
#infos
title.login = Login Page
label.username = Username
label.password = Password
item.submit = Submit
item.reset = Reset
message.success = You are successful to login in. Now you've entered the main page.
message.failure = You failed to login in. You need to register your information first.Create ApplicationResources_zh.properties using NoteBook.
--------------------------------------------------
ApplicationResources.properties
#一个简单的属性文件
title.login = 登录页面
label.username = 姓名
label.password = 密码
item.submit = 登录
item.reset = 重置
message.success = 你已经成功登陆。现进入了主页
message.failure = 你登录失败。现进入注册页面,请注册你的信息--------------------------------------------------
Using command:
, we get ApplicationResources_zh_CN.properties.
label.username = /u59d3/u540d
label.password = /u5bc6/u7801
item.submit = /u767b/u5f55
item.reset = /u91cd/u7f6e
message.success = /u4f60/u5df2/u7ecf/u6210/u529f/u767b/u9646/u3002/u73b0/u8fdb/u5165/u4e86/u4e3b/u9875
message.failure = /u4f60/u767b/u5f55/u5931/u8d25/u3002/u73b0/u8fdb/u5165/u6ce8/u518c/u9875/u9762/uff0c/u8bf7/u6ce8/u518c/u4f60/u7684/u4fe1/u606f
2. Confirm <message-resouce> in struts-config.xml
<message-resources parameter="ApplicationResources"/>3. Using <bean:message key="xxx" /> in .jsp files.
--------------------------------------------------
login.jsp
------------------------------
<%...@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html"%>
<%...@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean"%>
<%...@ page contentType="text/html; charset=gb2312"%>
<html:html>
<head>
<title><bean:message key="title.login"/></title>
</head>
<body>
<html:form action="loginAction" method="post">
<bean:message key="label.username"/> <html:text property="username" size="15" />
<font color="red"><html:errors property="name" /></font>
<br/>
<br/>
<bean:message key="label.password"/> <html:password property="password" size="15" />
<font color="red"><html:errors property="pwd" /></font>
<br />
<br />
<html:submit property="submit" >
<bean:message key="item.submit" />
</html:submit>
</html:form>
</body>
</html:html>
本文介绍如何在Struts框架中实现应用的多语言支持,通过创建资源文件并利用Struts提供的标签来显示不同语言的内容。
1013

被折叠的 条评论
为什么被折叠?



