*.properties文件是Java的配置文件,当然也可以来配置国际化资源。
1、在eclipse中创建properties文件
首先,在Window-preferences-General-ContextType中,选择Text-Java Properties File。若File associations中无“*.properties”,则去添加它。并且在Default encoding中改为“UTF-8”
如图所示:
然后在/src目录下创建properties文件(new-File),例:c.properties文件
c.properties文件为:
user=JSP消息:用户名
pass=JSP消息:密码
login=JSP消息:登录
示例的jsp文件为:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form>
<s:textfield name="username" key="user"></s:textfield>
<s:textfield name="password" key="pass"></s:textfield>
<s:submit key="login"></s:submit>
</s:form>
</body>
</html>
注意key属性
2、因为*.properties文件出现了非西欧字符,所以需用使用native2ascii命令去处理这份文件,生成的新文件名为 *_zh_CN.properties文件。
找到*.properties文件目录下,按下“SHIFT”+右键,打开命令窗口,输入:
native2ascii -encoding UTF-8 *.properties *_zh_CN.properties
按下回车,则生成*_zh_CN.properties文件
再在Eclipse中 refresh就可出现该文件
3、在struts.xml中加入如下语句去加载国际化资源文件
<constant name="struts.custom.i18n.resources" value="c"/>
(此例为c)
4、运行jsp文件
如图所示:
此时,已经转化为国际化资源信息