struts1.3默认读取.properties后缀的资源文件.struts-config.xml中配置如下: <message-resources parameter="com.bing.struts.message.ApplicationResources" />
ApplicationResources类似以下形式
one=一
two=二
three=三
若自定义资源工厂类,读取xml配置文件。struts-config.xml
<message-resources parameter="com.bing.struts.message.ApplicationResources" factory="com.bing.struts.util.XMLMessageResourcesFactory"/>
XMLMessageResourcesFactory
package com.bing.struts.util;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.PropertyMessageResourcesFactory;
public class XMLMessageResourcesFactory extends PropertyMessageResourcesFactory {
@Override
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;
}
/**
*
*/
private static final long serialVersionUID = 1L;
}
XMLPropertyMessageResources
package com.bing.struts.util;
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 = 1L;
public XMLPropertyMessageResources(MessageResourcesFactory factory,
String config) {
super(factory, config);
}
public XMLPropertyMessageResources(MessageResourcesFactory factory,
String config, boolean returnNull) {
super(factory, config, returnNull);
}
@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);
FileOutputStream out=null;
if (is != null) {
try {
props.loadFromXML(is);//此处修改官方代码,加载XML
} catch (IOException e) {
log.error("loadLocale()", e);
} finally {
try {
is.close();
// out.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));
}
}
}
}
ApplicationResources_zh_CN.xml
<?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="name">\u7f51\u8def\u5e76</entry>
<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>
测试:
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<bean:message key="vo.Packageinfo.packageName"/>