裝入Struts時,都會自動載入ApplicationResouces.properties這個檔案,即系統預設的資源檔。它在Struts-config.xml中的註冊語句是:
Code:
- <!-- 這是預設的資源檔 -->
- <message-resources parameter="prj700.ApplicationResources" />
prj700是包名。
編寫ApplicationResources.properties中的內容為:
Code:
- #井號開頭的是注釋
- #編寫的格式是: key=value
- info.input.account=please input account:
- info.input.password=please input password:
-
- #{0} 代表一個參數 {1} {2} {3} {4} 最多可以寫4個參數
- info.input=<font color/=red>please input {0}</font>
- #info.input.account是變數名 等號的右邊是值
在jsp中調用該資源檔的代碼為:
Code:
- <html:form action="/login">
- please input password : <html:password property="password"/><br/>
- please input account : <html:text property="account"/><br/>
- <html:submit/><html:cancel/>
- </html:form><HR>
-
- 1)用到資源檔的方法
- <html:form action="/login">
- <bean:message key="info.input.password"/> <html:password property="password"/><br/>
- <bean:message key="info.input.account"/><html:text property="account"/><br/>
- <html:submit/><html:cancel/>
- </html:form><hr>
-
- 2/用到資源檔的方法 ,通過參數來傳遞
- <html:form action="/login">
- <bean:message key="info.input" arg0="passord"/> <html:password property="password"/><br/>
- <bean:message key="info.input" arg0="account"/><html:text property="account"/><br/>
- <html:submit/><html:cancel/>
- </html:form><hr>
=====建立一中文版的資源檔ApplicationMyResources.properties ,並編寫內容 info.input=請輸入{0}
建立一個新的資源檔有2點要做的:
1.註冊 即在Struts中編寫如下語句:
Code:
- <!-- 其他的資源檔,通過Key來區分。 -->
- <message-resources parameter="prj700.ApplicationMyResources" key="TEST"></message-resources>
2.對該檔案進行轉碼。(使用jdk內建的工具 native2ascii -encode 編碼方式 源檔案 目標檔案)轉化後內容為:
Code:
- info.input=/u8BF7/u8F93/u5165 {0}
jsp中調用代碼為:
Code:
- 用到資源檔的方法 ,通過參數 中文版
- <!-- 此處buddle的值跟資源檔中的key值是一樣的 -->
- <html:form action="/login">
- <bean:message key="info.input" arg0="密碼" bundle="TEST"/> <html:password property="password"/><br/>
- <bean:message key="info.input" arg0="帳號" bundle="TEST"/><html:text property="account"/><br/>
- <html:submit/><html:cancel/>
- </html:form><hr>