通过 HTTP 加载 Java 资源包
2009-12-24 00:00:00 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤缂嶅﹪寮婚悢鍏尖拻閻庨潧澹婂Σ顔剧磼閻愵剙鍔ょ紓宥咃躬瀵鎮㈤崗灏栨嫽闁诲酣娼ф竟濠偽i鍓х<闁诡垎鍐f寖闂佺娅曢幑鍥灳閺冨牆绀冩い蹇庣娴滈箖鏌ㄥ┑鍡欏嚬缂併劎绮妵鍕箳鐎n亞浠鹃梺闈涙搐鐎氫即鐛崶顒夋晬婵絾瀵ч幑鍥蓟閻斿摜鐟归柛顭戝枛椤牆顪冮妶搴′簼缂侇喗鎸搁悾鐑藉础閻愬秵妫冮崺鈧い鎺戝瀹撲礁鈹戦悩鎻掝伀缁惧彞绮欓弻娑氫沪閹规劕顥濋梺閫炲苯澧伴柟铏崌閿濈偛鈹戠€n€晠鏌嶆潪鎷屽厡闁汇倕鎳愮槐鎾存媴閸撴彃鍓卞銈嗗灦閻熲晛鐣烽妷褉鍋撻敐搴℃灍闁绘挻娲橀妵鍕箛闂堟稐绨肩紓浣藉煐濮樸劎妲愰幘璇茬闁冲搫鍊婚ˇ鏉库攽椤旂》宸ユい顓炲槻閻g兘骞掗幋鏃€鐎婚梺瑙勬儗閸樺€熲叺婵犵數濮烽弫鍛婃叏椤撱垹纾婚柟鍓х帛閳锋垶銇勯幒鍡椾壕缂備礁顦遍弫濠氱嵁閸℃稒鍊烽柛婵嗗椤旀劕鈹戦悜鍥╃У闁告挻鐟︽穱濠囨嚃閳哄啰锛滈梺褰掑亰閸欏骸鈻撳⿰鍫熺厸閻忕偟纭堕崑鎾诲箛娴e憡鍊梺纭呭亹鐞涖儵鍩€椤掑啫鐨洪柡浣圭墪閳规垿鎮欓弶鎴犱桓闂佸湱枪閹芥粎鍒掗弮鍫熷仺缂佸顕抽敃鍌涚厱闁哄洢鍔岄悘鐘绘煕閹般劌浜惧┑锛勫亼閸婃牠宕濋敃鈧…鍧楀焵椤掍胶绠剧€光偓婵犱線鍋楀┑顔硷龚濞咃絿妲愰幒鎳崇喓鎷犻懠鑸垫毐闂傚倷鑳舵灙婵炲鍏樺顐ゆ嫚瀹割喖娈ㄦ繝鐢靛У绾板秹寮查幓鎺濈唵閻犺櫣灏ㄥ銉р偓瑙勬尭濡繂顫忛搹鍦<婵☆垰鎼~宥囩磽娴i鍔嶉柟绋垮暱閻g兘骞嬮敃鈧粻濠氭偣閸パ冪骇鐎规挸绉撮—鍐Χ閸℃ê闉嶇紓浣割儐閸ㄥ墎绮嬪澶嬪€锋い鎺嶇瀵灝鈹戦埥鍡楃仯闁告鍕洸濡わ絽鍟崐鍨叏濡厧浜鹃悗姘炬嫹

JSP 在 MessagesBean 实例中设置几个属性,然后遍历 MessagesBean 创建的 messagesMap,并显示格式化的消息(清单 4)。
清单 4. MessagesBean.javapublic Map<String,String> getMessagesMap(){
messages = new HashMap<String, Object[]>();
// The message keys and the arguments are stored in
// a HashMap for the purposes of this sample.
// In the real world, both the message keys and their arguments could be
// persisted to a database by event emitters living in many different
// Web Applications which could be deployed on different hosts.
// A single, centralized display widget, living on any server anywhere,
// could retrieve the message keys and their arguments from the database,
// get the messages from the appropriate Web Application resource bundles,
// and then format and display the messages.
messages.put("autoparts.parts.received",
new Object[]{"Detroit"});
messages.put("autoparts.parts.shipped",
new Object[]{"Cleveland"});
messages.put("autosales.invoice.received",
new Object[]{"TopNotch Motors", 10});
messages.put("autosales.shipment.received",
new Object[]{"TopNotch Motors", "Lamborghini"});
Set<String> messagesKeys = messages.keySet();
Iterator<String> messagesIter = messagesKeys.iterator();
messagesMap = new HashMap<String,String>();
while(messagesIter.hasNext()) {
String key = messagesIter.next();
Object[] values = messages.get(key);
int dotIndex = key.indexOf('.');
if (dotIndex>1) {
String productName= key.substring(0, dotIndex);
String msg = getString(key,productName, this.locale );
String formattedParams = MessageFormat.format(msg, values);
messagesMap.put(key, formattedParams);
}
}
return messagesMap;
}
/**
* Returns the string in a resource bundle for key, product and locale
*
* @param messageKey a key
* @param product the identifier for a specific resource bundle
* @param locale the locale of the client
* @return String a value which is assigned to the key
*/
public String getString(String messageKey, String product, String locale) {
try {
// Load bundle using RemoteResourceBundleLoader.
ResourceBundle bundle = ResourceBundle.getBundle(
BASE_BUNDLE_PACKAGE+"."+product,
new Locale(locale),
new RemoteResourceBundleLoader(this.scheme, this.host,
this.port, this.refererHeader));
String messageString=bundle.getString(messageKey);
return messageString;
} catch (MissingResourceException e) {
return null;
}
}
当 JSP 访问 bean 的 messagesMap 属性时,bean 为每个硬编码消息键调用 getString()。
在 JDK 6 中,ResourceBundle.getBundle() 方法把 ResourceBundle.Control 类作为一个参数。在清单 4 的 getString() 方法中,您把类 RemoteResourceBundleLoader 的一个新实例作为 ResourceBundle.getBundle() 的第三个参数进行传递。RemoteResourceBundleLoader 扩展 ResourceBundle.Control。对 ResourceBundle.getBundle() 的调用返回了一个资源包。通过调用 bundle.getString(messageKey),这个资源包被用来获取消息键的消息。
在您获得消息后,使用 MessageFormat.format() 格式化消息,然后把格式化的消息放到由 JSP 使用的 messagesMap 中。
结束语
本文向你演示了如何通过 HTTP 加载 Java 资源包。在真实的情景中,消息键和它们的参数都会被许多不同的 Web 应用程序(可能部署在不同的主机中)中的事件发送器持久化到数据库中。一个单一、集中化的显示小部件(位于任何地方的任何服务器)能够从数据库中检索消息键和它们的参数,从适当的 Web 应用程序资源包中获取消息,然后格式化消息并进行显示。
本文示例源代码或素材下载
更多精彩
赞助商链接