在网上找了一下资料,很多文章推荐使用native2ascii命令。
个人觉得很不方便,因为开发过程中资源文件修改比较频繁,每次都要运行脚本。
用PropertiesEditor插件。
还是很不方便,每个开发客户端都要装,很麻烦。
另外,资源文件,特别是国际化的时候,通常开发人员都喜欢用UTF-8来编码,以方便修改。但调试的时候就会出问题。
解决办法思路:因为struts是使用Classloader.load(InputStream)来加载资源文件的。所以必须继承修改加载资源文件实现类org.apache.struts.util.PropertyMessageResources的loadLocale方法。第二,兼顾资源文件修改的方便性,最好使资源文件xml化。这样只要标准的xml IDE就可以方便修改,从根本上避免的字符集和IDE相关联的问题。而且可以利用JDK1.5的特性:Properties有一个loadFromXML(InputStream)方法可以方便加载。
解决办法:继承和修改资源文件加载相关的两个实现类:
com.yourcompany.struts.util.XMLPropertyMessageResources继承
org.apache.struts.util.PropertyMessageResources
package com.yourcompany.struts.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import org.apache.struts.util.MessageResourcesFactory;
import org.apache.struts.util.PropertyMessageResources;
public class XMLPropertyMessageResources extends PropertyMessageResources {
/**
*
*/
private static final long serialVersionUID = 3701712123553100102L;
public XMLPropertyMessageResources(MessageResourcesFactory factory,
String config, boolean returnNull) {
super(factory, config, returnNull);
}
public XMLPropertyMessageResources(MessageResourcesFactory factory,
String config) {
super(factory, config);
}
@Override
protected synchronized void loadLocale(String localeKey) {
if (log.isTraceEnabled()) {
log.trace("loadLocale(" + localeKey + ")");
}
// Have we already attempted to load messages for this locale?
if (locales.get(localeKey) != null) {
return;
}
locales.put(localeKey, localeKey);
// Set up to load the property resource for this locale key, if we can
String name = config.replace('.', '/');
if (localeKey.length() > 0) {
name += ("_" + localeKey);
}
name += ".xml";//此处修改官方代码,便于IDE环境编辑XML
InputStream is = null;
Properties props = new Properties();
// Load the specified property resource
if (log.isTraceEnabled()) {
log.trace(" Loading resource '" + name + "'");
}
ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
is = classLoader.getResourceAsStream(name);
if (is != null) {
try {
props.loadFromXML(is);//此处修改官方代码,加载XML
} catch (IOException e) {
log.error("loadLocale()", e);
} finally {
try {
is.close();
} catch (IOException e) {
log.error("loadLocale()", e);
}
}
if (log.isTraceEnabled()) {
log.trace(" Loading resource completed");
}
} else {
if (log.isWarnEnabled()) {
log.warn(" Resource "+name+" Not Found.");
}
}
// Copy the corresponding values into our cache
if (props.size() < 1) {
return;
}
synchronized (messages) {
Iterator names = props.keySet().iterator();
while (names.hasNext()) {
String key = (String) names.next();
if (log.isTraceEnabled()) {
log.trace(" Saving message key '"
+ messageKey(localeKey, key));
}
messages.put(messageKey(localeKey, key), props.getProperty(key));
}
}
}
}
com.yourcompany.struts.util.XMLMessageResourcesFactory继承
org.apache.struts.util.PropertyMessageResourcesFactory
package com.yourcompany.struts.util;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.PropertyMessageResourcesFactory;
public class XMLMessageResourcesFactory extends PropertyMessageResourcesFactory {
public MessageResources createResources(String config) {
XMLPropertyMessageResources messageResources =
new XMLPropertyMessageResources(this, config, this.returnNull);//此处修改官方代码,使用修改后的资源加载类
String mode = null;
if (getConfig() != null) {
mode = getConfig().getProperty("mode");
}
messageResources.setMode(mode);
return messageResources;
}
}
修改struts-config.xml的此行为:
<message-resources parameter="com.yourcompany.struts.message.ApplicationResources" factory="com.yourcompany.struts.util.XMLMessageResourcesFactory"/>
你的资源文件改为如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Resources for default
</comment>
<entry key="commons.validate.notnull">can't be empty</entry>
<entry key="vo.Packageinfo.packageId">Package ID</entry>
<entry key="vo.Packageinfo.packageAlias">Package Alias</entry>
<entry key="vo.Packageinfo.packageName">Package Name</entry>
<entry key="vo.Packageinfo.packagePrices">Package Prices</entry>
<entry key="vo.Packageinfo.productLimit">Product Limit</entry>
<entry key="vo.Packageinfotbl.description">Description</entry>
</properties>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Resources for zh_CN
</comment>
<entry key="commons.validate.notnull">不能为空</entry>
<entry key="vo.Packageinfo.packageId">套餐ID</entry>
<entry key="vo.Packageinfo.packageAlias">套餐别名</entry>
<entry key="vo.Packageinfo.packageName">套餐名</entry>
<entry key="vo.Packageinfo.packagePrices">套餐价格</entry>
<entry key="vo.Packageinfo.productLimit">允许发布产品数量</entry>
<entry key="vo.Packageinfo.description">描述信息</entry>
</properties>
如果已经有经过native2ascii转换过的资源文件的。可以使用一下代码转换:
Properties props = new Properties();
props.load(new FileInputStream(...));
props.storeToXml(new FileOutputStream(...));
.....
总结:如果是团队开发,修改资源文件的加载类,只需要做一次就可以让团队开发人员省却重复的工作。
分享到:
相关推荐
有关struts1.3方面的常用解说,对常用配置、常用标签的使用等方面都有详细的介绍!
struts1.3jar包
struts1.3 包含action,form,struts-config.xml,实现了,新手可以考虑看下,老鸟就算了。价值,你懂得!
struts1.3+spring3.0+hibernate3.3简单项目示例
Struts1.3的全部架包,非常全,包含:struts-core-1.3.10.jar,struts-tiles-1.3.10.jar,struts-taglib-1.3.10.jar,struts-faces-1.3.10.jar,struts-extras-1.3.10.jar........
内含Struts1.3 API、Struts2.0 API、Struts1.3标签、Hibernate3.1教程、Hibernate3.1API、Spring3.0 API、Spring3.1API。SSH应有尽有,API均是CHM格式,方便使用。教程为pdf格式。
Struts1.3源代码Struts1.3源代码Struts1.3源代码Struts1.3源代码
开发者使用struts1进行web开发时libjar包,在这里。希望对你们有所帮助!
此文件中包含了struts开发中所有的jar文件
struts1.3api参考文档,是英文版的!想了解struts可以看下
Ibatis2.3,Struts1.3,Spring2.5框架整合简单实例代码,用了一周的业余时间,终于调通了。有需要的可以下载。
上面是一些Struts1.3的相关jar包和API
Struts1.3+hibernate+Spring3Struts1.3+hibernate+Spring3Struts1.3+hibernate+Spring3Struts1.3+hibernate+Spring3Struts1.3+hibernate+Spring3Struts1.3+hibernate+Spring3Struts1.3+hibernate+Spring3Struts1.3+...
struts1.3相关jar包,把这些jar包导进去,struts1.3就可以使用了
struts_1.3 src 全文件 Apache Struts Copyright 2000-2007 The Apache Software Foundation This product includes software developed by The Apache Software Foundation (http://www.apache.org/).
Struts1.3 帮助文档APIStruts1.3 帮助文档APIStruts1.3 帮助文档APIStruts1.3 帮助文档APIStruts1.3 帮助文档APIStruts1.3 帮助文档APIStruts1.3 帮助文档APIStruts1.3 帮助文档APIStruts1.3 帮助文档API
可以实现书籍的增删改查和分页功能,采用MyEclipse+SqlServer
struts1.3.rar
这是从apache官方网下载的struts1.3 api文档。等以后有时间再做成chm的。