两编文章一起看你,如果不成功的话。如下是我自己写的一个示例:
message_en_US.properties
message_zh_CN.properties
struts.properties
以上三个文件都放在src根目录下。
其中添加struts2.0时会有两个文件产生:struts.xml、struts.properties。
struts2.0支持国际化需要在struts.xml和struts.properties进行如下配置:
struts.xml配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<constant name="struts.custom.i18n.resources" value="message"></constant><!-- 注意这行代码,它是用来支持国际化的 -->
</struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>代码,且必须在<struts></struts>之间。
struts.properties配置如下:
struts.custom.i18n.resources=message
在新建的struts.properties空白文件中加上如上代码,以上文件的意思是用于读取以message开头的属性文件。
如:message_en_US.properties、message_zh_CN.properties等。
message_en_US.properties配置如下:
HelloWorld=Hello World!
message_zh_CN.properties配置如下:
HelloWorld=你好,世界!
在此和大家分享一个不错的编写properties文件的Eclipse插件(plugin),有了它我们在编辑一些简体中文、繁体中文等Unicod文本时,就不必再使用native2ascii编码了。您可以通过Eclipse中的软件升级(Soft Update)安装此插件,步骤如下:
1. 展开Eclipse的Help菜单,将鼠标移到Software Update子项,在出现的子菜单中点击Find and Install;
2. 在Install/Upadte对话框中选择 Search for new features to install,点击Next;
3. 在Install对话框中点击New Remote site;
4. 在New Update Site 对话框中的Name填入:“ProEdit”或其它任意非空间字符串,在URL里填入http://proedit.sourceforge.jp/eclipse/updates;
5. 在Site to include to search列表中,除上一步加入的site外的其它选项都去掉,点击Finsih;
6. 在弹出的Upadtes对话框中的Select the features to indstall 列表中将所有结果为"3.1.x"的选项去掉(适用于3.2版本的朋友)。
7. 点击Finish关闭对话框;
8. 在下载后,同意安装,再按提示重启Eclipse,在工具条看到形似vi的按钮表示安装插件可用。此时,Eclipse中所有properties文件的文件名前有绿色的P的图标作为标识。
struts2.0支持国际化lib下的jar包如下:
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.13.jar
log4j-1.2.14.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 开始配置struts2.0的支持 -->
<display-name>Struts 2.0 Hello World</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 结束配置struts2.0的支持 -->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
jsp页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="show" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Hello Word</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<show:i18n name="message">
<show:text name="HelloWorld"></show:text>
</show:i18n>
<!--<show:property value="%{getText('HelloWorld')}"/>-->
</body>
</html>