Struts2 資源文件中訪問消息的組合

問題:表單驗證時,如果驗證錯誤,將提示"用戶名 不能爲空","密碼不能爲空", 其中,用戶名密碼 可作爲輸入提示標籤(label)的內容.則如何在資源文件中定義消息能實現複用
只所以,把這個問題拿出來是論壇中很多人提到 且官網解釋經自己的測試存在問題

解決方案:

方式1:

         lbl.username=用戶名

         lbl.password=密碼

         prompt.requiredString=不能爲空

         prompt.usernameRequired=%{getText('lbl.username')}%{getText('prompt.requiredString')}或=%{getText('lbl.username')+getText('prompt.requiredString')}

         promt.passwordRequired=%{getText('lbl.password)}%{getText('prompt.requiredString')}或=%{getText('lbl.password)+getText('prompt.requiredString')}

方式2:

         將方式1的 用戶名和密碼的提示方式改爲prompt.required=%{getText(fieldName)} 不能爲空或%{getText(fieldName)}%{getText('prompt.requiredString')}

Note:

         在Struts的官方文檔中,如果想在資源文件中的一條引用另一條,則使用%{lbl.username} 這種方式經編碼驗證不能顯示----見Struts2解壓文件《struts-2.2.3.1\docs\WW\message-resource-files.html》

         getText中的 屬性鍵(key) 必須要使用引號

        

?缺源碼,文字需要重新組織

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章