Struts中非常常用的有這樣的一個標簽
<bean:message key=
眾所周知
訪問在struts
<message
根據以上的定義
通過這段代碼
A
B
C
D
E
// Set up to load the property resource for this locale key
String name = config
if (localeKey
name +=
}
name +=
InputStream is = null;
Properties props = new Properties();
// Load the specified property resource
if (log
log
}
ClassLoader classLoader = Thread
if (classLoader == null) {
classLoader = this
}
is = classLoader
if (is != null) {
try {
props
} catch (IOException e) {
log
} finally {
try {
is
} catch (IOException e) {
log
}
}
}
D步驟中的load方法可以參看JDK的幫助文檔
另外
public String getMessage(Locale locale
if (log
log
}
// Initialize variables we will require
String localeKey = localeKey(locale);
String originalKey = messageKey(localeKey
String messageKey = null;
String message = null;
int underscore =
boolean addIt = false; // Add if not found under the original key
// Loop from specific to general Locales looking for this message
while (true) {
// Load this Locale
loadLocale(localeKey);
// Check if we have this key for the current locale key
messageKey = messageKey(localeKey
synchronized (messages) {
message = (String) messages
if (message != null) {
if (addIt) {
messages
}
return (message);
}
}
// Strip trailing modifiers to try a more general locale key
addIt = true;
underscore = localeKey
if (underscore <
break;
}
localeKey = localeKey
}
// Try the default locale if the current locale is different
if (!defaultLocale
localeKey = localeKey(defaultLocale);
messageKey = messageKey(localeKey
loadLocale(localeKey);
synchronized (messages) {
message = (String) messages
if (message != null) {
messages
return (message);
}
}
}
// As a last resort
這裡可以看到Struts最後將localeKey賦空
這樣資源文件就是$Filename
localeKey =
messageKey = messageKey(localeKey
loadLocale這個方法將讀取資源文件
這個方法的代碼在上面已經列出來了
loadLocale(localeKey);
synchronized (messages) {
message = (String) messages
if (message != null) {
messages
return (message);
}
}
// Return an appropriate error indication
if (returnNull) {
return (null);
} else {
return (
}
}
至於這個$Locale的值是多少
在org
public static Locale getUserLocale(HttpServletRequest request
Locale userLocale = null;
HttpSession session = request
if (locale == null) {
locale = Globals
}
// Only check session if sessions are enabled
if (session != null) {
userLocale = (Locale) session
}
if (userLocale == null) {
// Returns Locale based on Accept
userLocale = request
}
return userLocale;
}
可以看出
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27882.html