Struts資源檔的調用(.properties)

來源:互聯網
上載者:User

        裝入Struts時,都會自動載入ApplicationResouces.properties這個檔案,即系統預設的資源檔。它在Struts-config.xml中的註冊語句是:

Code:
  1. <!-- 這是預設的資源檔 -->  
  2.  <message-resources parameter="prj700.ApplicationResources" />  

prj700是包名。

編寫ApplicationResources.properties中的內容為:

Code:
  1. #井號開頭的是注釋   
  2. #編寫的格式是:  key=value     
  3. info.input.account=please input account:   
  4. info.input.password=please input password:   
  5.   
  6. #{0} 代表一個參數   {1} {2} {3} {4}  最多可以寫4個參數   
  7. info.input=<font color/=red>please input {0}</font>  
  8. #info.input.account是變數名 等號的右邊是值

 在jsp中調用該資源檔的代碼為:

Code:
  1. <html:form action="/login">   
  2.             please input password : <html:password property="password"/><br/>   
  3.             please input account : <html:text property="account"/><br/>   
  4.             <html:submit/><html:cancel/>   
  5.         </html:form><HR>   
  6.         
  7.           1)用到資源檔的方法   
  8.         <html:form action="/login">   
  9.             <bean:message key="info.input.password"/> <html:password property="password"/><br/>   
  10.             <bean:message key="info.input.account"/><html:text property="account"/><br/>   
  11.             <html:submit/><html:cancel/>   
  12.         </html:form><hr>   
  13.            
  14.         2/用到資源檔的方法 ,通過參數來傳遞   
  15.         <html:form action="/login">   
  16.             <bean:message key="info.input" arg0="passord"/> <html:password property="password"/><br/>   
  17.             <bean:message key="info.input"  arg0="account"/><html:text property="account"/><br/>   
  18.             <html:submit/><html:cancel/>   
  19.         </html:form><hr>  

=====建立一中文版的資源檔ApplicationMyResources.properties  ,並編寫內容 info.input=請輸入{0}

建立一個新的資源檔有2點要做的:

1.註冊  即在Struts中編寫如下語句:

Code:
  1. <!-- 其他的資源檔,通過Key來區分。 -->  
  2.    <message-resources parameter="prj700.ApplicationMyResources" key="TEST"></message-resources>  

2.對該檔案進行轉碼。(使用jdk內建的工具 native2ascii -encode  編碼方式  源檔案  目標檔案)轉化後內容為:

Code:
  1. info.input=/u8BF7/u8F93/u5165 {0}  

jsp中調用代碼為:

Code:
  1. 用到資源檔的方法 ,通過參數  中文版     
  2.         <!-- 此處buddle的值跟資源檔中的key值是一樣的 -->  
  3.         <html:form action="/login">  
  4.             <bean:message key="info.input" arg0="密碼" bundle="TEST"/> <html:password property="password"/><br/>  
  5.             <bean:message key="info.input"  arg0="帳號" bundle="TEST"/><html:text property="account"/><br/>  
  6.             <html:submit/><html:cancel/>  
  7.         </html:form><hr>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.