鏈€杩戝湪瀛︿範struts2锛屽涔犺祫鏂欐槸鏉庡垰钁楃殑銆妔truts2鏉冨▉鎸囧崡銆嬶紝杩欐湰涔﹀啓寰楅潪甯稿ソ锛岄潪甯告湁瀛︿範浠峰€箋€傛垜鍦ㄥ涔犺繃绋嬩腑锛岃嚜宸辮窡鐫€鍋氫簡浜涗緥瀛愩€備笅闈㈠氨鏄叧浜庡湪struts2涓厑璁哥敤鎴瘋嚜琛岄€夋嫨绋嬪簭璇█鐨勫師鐞嗗拰绀轟緥锛?
鍦ㄥ緢澶氭垚鐔熺殑鍟嗕笟杞歡涓紝鍙互璁╃敤鎴瘋嚜鐢卞垏鎹㈣瑷€锛屽綋鐢ㄦ埛杩涘叆绯葷粺鏃跺€欙紝鍙互鍑虹幇涓€涓笅鎷夊垪琛ㄦ锛岃鐢ㄦ埛閫夋嫨璇█锛屼竴鏃︾敤鎴烽€夋嫨浜嗚嚜宸遍渶瑕佷嬌鐢ㄧ殑璇█鐜锛屾暣涓郴缁熺殑璇█鐜灏嗕竴鐩存槸杩欑璇█鐜銆? Struts2涔熷彲浠ュ厑璁哥敤鎴瘋嚜琛岄€夋嫨绋嬪簭璇█銆傝€屼笖锛屽洜涓篠truts2鐨勬敮鎸侊紝鍦ㄧ▼搴忎腑鑷閫夋嫨璇█鐜灏嗗彉寰楁洿鍔犵畝鍗曘€?
涓€. Struts2鍥介檯鍖栫殑杩愯鏈哄埗
鍦⊿truts 2涓紝鎴戜滑鍙互閫氳繃ActionContext.getContext().setLocale(Locale arg)鍙互璁劇疆鐢ㄦ埛鐨勯粯璁よ瑷€銆備笉杩囷紝杩欑鏂瑰紡瀹屽叏鏄竴绉嶆墜鍔ㄦ柟寮忥紝鑰屼笖闇€瑕佺紪绋嬪疄鐜般€? 涓轟簡绠€鍖栬缃敤鎴烽粯璁よ瑷€鐜锛孲truts 2鎻愪緵浜嗕竴涓悕i18n鐨勬嫤鎴櫒锛圛nterceptor锛夛紝骞朵笖灏嗗叾浣忔敞鍐屽湪榛樿鐨勬嫤鎴櫒鏍堜腑锛坉efaultStack锛夈€備笅闈㈡槸Struts2鐨勯粯璁ゆ嫤鎴櫒鏍堢殑閰嶇疆鐗囨锛屼唬鐮佷腑绮椾綋瀛楄〃绀虹殑灏辨槸i18n鐨勬嫤鎴櫒
<interceptor-stack name="defaultStack">
聽<interceptor-ref name="exception"/>
聽<interceptor-ref name="alias"/>
聽<interceptor-ref name="servlet-config"/>
聽<interceptor-ref name="prepare"/>
聽<interceptor-ref name="i18n"/>
聽<interceptor-ref name="chain"/>
聽<interceptor-ref name="debugging"/>
聽<interceptor-ref name="profiling"/>
聽<interceptor-ref name="scoped-model-driven"/>
聽<interceptor-ref name="model-driven"/>
聽<interceptor-ref name="fileUpload"/>
聽<interceptor-ref name="checkbox"/>
聽<interceptor-ref name="static-params"/>
聽<interceptor-ref name="params">
聽聽<param name="excludeParams">dojo\..*</param>
聽</interceptor-ref>
聽<interceptor-ref name="conversionError"/>
聽<interceptor-ref name="validation">
聽聽聽聽 <param name="excludeMethods">input,back,cancel,browse</param>
聽</interceptor-ref>
聽<interceptor-ref name="workflow">
聽聽聽聽 <param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
i18n鎷︽埅鍣ㄥ湪鎵цAction鏂規硶鍓嶏紝鑷姩鏌ユ壘璇鋒眰涓殑涓€涓悕涓簉equest_locale鐨勫弬鏁般€傚鏋滆鍙傛暟瀛樺湪锛屾嫤鎴櫒灏卞皢鍏朵綔涓哄弬鏁幫紝骞惰漿鎹㈡垚Locale瀵矽薄锛屽苟灏嗗叾璁句負鐢ㄦ埛榛樿鐨凩ocale锛堜唬琛ㄨ瑷€/鍥藉鐜锛夈€? 闄ゆ涔嬪锛宨18n鎷︽埅鍣ㄨ繕浼氬皢涓婇潰鐢熸垚鐨凩ocale瀵矽薄淇濆瓨鍦ㄧ敤鎴稴ession鐨勫悕涓衡€淲W_TRANS_I18N_LOCALE鈥濈殑灞炴€т腑銆備竴鏃︾敤鎴稴ession涓瓨鍦ㄤ竴涓悕涓衡€淲W_TRANS_I18N_LOCALE鈥濈殑灞炴€э紝鍒欒灞炴€ф寚瀹氱殑Locale灏嗕細浣滀負娴忚鑰呯殑榛樿Locale銆?
浜? 鍒涘緩涓嬫媺鍒楄〃妗? 鍩轟簬鍓嶉潰鐨勪粙缁嶏紝涓轟簡瀹炵幇璁╃敤鎴瘋嚜琛岄€夋嫨绋嬪簭璇█鐨勫姛鑳斤紝鍙渶鎻愪緵涓€涓笅鎷夊垪琛ㄦ锛岃涓嬫媺鍒楄〃妗嗕腑鍒楀嚭鏈簲鐢ㄦ墍鏀寔鐨勫悇绉嶈瑷€锛屽苟涓旓紝褰撶敤鎴烽€夋嫨涓嬫媺鍒楄〃妗嗕腑鏌愪竴椤規椂锛岀郴缁熷皢璇ヤ笅鎷夐」鐨勫€間綔涓簉equest_locale鍙傛暟鎻愪氦缁橲truts2绯葷粺锛? 涓烘锛屾垜浠皢绯葷粺鎵€鏀寔鐨勮瑷€鏀懼叆涓€涓狹ap涓紝閫氳繃鍦↗SP椤甸潰涓凱浠hMap瀵矽薄锛岄€氳繃杩欑鏂瑰紡锛屽氨鍙互鍦ㄩ〉闈笂鍒楀嚭绯葷粺鎵€鏀寔鐨勫叏閮ㄨ瑷€锛屽苟璁╃敤鎴瘋嚜鐢遍€夋嫨銆? 涓嬮潰瀹氫箟浜咼avaBean锛岃繖涓狫avaBean閲屼繚瀛樹簡褰撳墠搴旂敤鎵€鏀寔鐨勫叏閮ㄨ瑷€锛岃JavaBean鐨勪唬鐮佸涓嬶細
//璇avaBean瀛樻斁浜嗙郴缁熸墍鏀寔鐨勫叏閮ㄨ瑷€銆? public class Locales
{
聽//鍥犱負鏈疄渚嬩篃闇€瑕佸疄鐜闆浗闄呭寲锛屾墍浠ヤ嬌鐢╟urrent浣滀負鐢ㄦ埛褰撳墠鐨凩ocale
聽private Locale current;
聽//鍙栧緱鐢ㄦ埛褰撳墠Locale鐨剆etter鏂規硶
聽public void setCurrent(Locale cur)
聽{
聽聽this.current = cur;
聽}
聽//鍙栧緱鏈郴缁熸墍鏀寔鐨勫叏閮ㄨ瑷€
聽聽聽 聽public Map<String, Locale> getLocales()
聽{
聽聽//灏嗗綋鍓嶇郴缁熸敮鎸佺殑鍏ㄩ儴璇█淇濇寔鍦∕ap瀵矽薄涓? 聽聽聽聽聽聽聽 聽Map<String, Locale> locales = new Hashtable<String, Locale>();
聽聽ResourceBundle bundle = ResourceBundle.getBundle("messageResource" , current);
聽聽//娣誨姞褰撳墠绯葷粺鏀寔鐨勮瑷€锛宬ey鏄郴缁熸敮鎸佽瑷€鐨勬樉绀哄悕瀛楋紝value鏄郴缁熸敮鎸佽瑷€鐨凩ocale瀹炰緥
聽聽聽聽聽聽聽 聽locales.put(bundle.getString("usen"), Locale.US);
聽聽聽聽聽聽聽 聽locales.put(bundle.getString("zhcn"), Locale.CHINA);
聽聽聽聽聽聽聽 聽return locales;
聽聽聽 }
}
鍦ㄤ笂闈avaBean涓紝鎴戜滑浣跨敤浜嗕竴涓狹ap瀵矽薄鏉ヤ繚瀛樻墍鏈夌敤鎴鋒敮鎸佺殑璇█锛岃Map瀵矽薄鐨刱ey鏄墍鏀寔璇█鐨勬樉绀哄悕瀛楋紝鑰岃Map瀵矽薄鐨剉alue鏄墍鏀寔璇█鐨凩ocale瀹炰緥銆傚簲鐢ㄦ敮鎸佽瑷€鐨勬樉绀哄悕瀛楋紝涔熸槸閫氳繃鍥介檯鍖栨秷鎭潵鐢熸垚鐨勩€? 涓€鏃﹀畾涔変簡璇avaBean涔嬪悗锛屽氨鍙互鍦↗SP椤甸潰涓垱寤鴻JavaBean鐨勫疄渚嬶紝骞朵負鍏朵紶鍏ヤ竴涓猚urrent鍙傛暟锛屽喅瀹氳JavaBean涓殑Locale鍙傛暟锛屽氨鍙互鏍規嵁璇ocale鍙傛暟鏉ュ喅瀹氭€庢牱鏄劇ず绯葷粺鎵€鏀寔璇█鏄劇ず鍚嶇О銆? 涓轟簡鍦↗SP椤甸潰涓嬌鐢ㄨJavaBean瀹炰緥锛屼嬌鐢ㄤ笅闈㈡爣绛炬潵鍒涘緩璇avaBean瀹炰緥銆? <!-- 浣跨敤lee.Locales鍒涘緩locales瀹炰緥 -->
<s:bean id="locales" name="lee.Locales">
聽<!-- 涓簂ocales瀹炰緥杩欑current鍙傛暟鍊?-->
聽<s:param name="current"
聽聽value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"/>
</s:bean>
涓婇潰鏍囩鍒涘緩浜唋ee.Locales绫葷殑locales瀹炰緥锛屽苟涓鴻瀹炰緥浼犲叆浜哻urrent鍙傛暟鍊鹼紝璁劇疆璇ュ弬鏁闆€兼椂浣跨敤浜嗕笁鐩繍绠楃锛屽厛鍒ゆ柇SESSION_LOCALE鏄惁涓虹┖锛屽鏋滆SESSION_LOCALE涓虹┖锛屽垯杩斿洖ValueStack涓璴ocale灞炴€у€鹼紙鍗崇敤鎴鋒祻瑙堝櫒璁劇疆鐨凩ocale锛夛紱濡傛灉SESSION_LOCALE涓嶄負绌猴紝鍒欒繑鍥炶SESSION_LOCALE鐨勫€鹼紙鍗崇敤鎴烽€夋嫨鐨凩ocale锛夈€? 涓轟簡璁╄椤甸潰涓寘鍚玈ESSION_LOCALE锛屼嬌鐢⊿truts2鐨?lt;s:set .../>鏍囩灏嗙敤鎴稴ession涓殑鈥淲W_TRANS_I18N_LOCALE鈥濆睘鎬у€艱缃垚SESSION_LOCALE銆? 涓嬮潰鏄畬鎴愯璁劇疆鐨勬爣绛撅細
<!-- 灏嗙敤鎴稴ession涓殑鈥淲W_TRANS_I18N_LOCALE鈥濆睘鎬у€艱缃垚SESSION_LOCALE銆?-->
<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>
涓嬮潰鏄selectlanguage.jsp椤甸潰鐨勪唬鐮侊細
<%@taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript">
function langSelecter_onChanged()
{
聽document.getElementById("langForm").submit();
}
</script>
<!-- 灏嗙敤鎴稴ession涓殑鈥淲W_TRANS_I18N_LOCALE鈥濆睘鎬у€艱缃垚SESSION_LOCALE銆?-->
<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>
<!-- 浣跨敤lee.Locales鍒涘緩locales瀹炰緥 -->
<s:bean id="locales" name="lee.Locales">
聽<!-- 涓簂ocales瀹炰緥杩欑current鍙傛暟鍊?-->
聽<s:param name="current"
聽聽value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"/>
</s:bean>
<!-- 璁╃敤鎴烽€夋嫨璇█鐨勮〃鍗?-->
<form action="<s:url/>" id="langForm"
聽聽聽 聽style="background-color:#bbbbbb; padding-top: 4px; padding-bottom: 4px;">
聽<!-- 杈撳嚭鍥介檯鍖栨彁绀?-->
聽聽聽 聽<s:text name="languag"/>
聽<!-- 浣跨敤s:select鏍囩杩唬locales瀹炰緥鐨刲ocales Map灞炴€?-->
聽<s:select label="Language" list="#locales.locales" listKey="value" listValue="key"
聽聽聽聽聽聽聽 value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"
聽聽聽聽聽聽聽 name="request_locale" id="langSelecter"
聽聽聽聽聽聽聽 慰nchange="langSelecter_onChanged()" theme="simple"/>
</form>
娉ㄦ剰锛氫笂闈㈤〉闈腑澶ч噺浣跨敤浜哠truts2鐨勬爣绛撅紝鍏充簬Struts2鏍囩鐨勮缁嗙敤娉曪紝璇峰弬鑰冩湰涔︾鍗佺珷鐨勮瑙c€? 涓婇潰椤甸潰鐨勫師鐞嗘槸锛氫嬌鐢?lt;s:set .../>鏍囩瀹炰緥鍖栦竴涓狶ocales瀵矽薄锛屽苟浣跨敤<s:select ..../>鏍囧墠鏉ユ樉绀鴻Locales瀵矽薄鐨刲ocales锛圡ap绫誨瀷锛夊睘鎬э紝 <s:select ..../>鍙互浣跨敤涓€涓笅鎷夊垪琛ㄦ鏉ユ樉绀篗ap绫誨瀷鐨勯泦鍚堬紝鏈簲鐢ㄥ皢璇ap鐨刱ey杈撳嚭鎴愪笅鎷夊垪琛ㄩ」鐨勬樉绀哄悕绉幫紝灏嗚Map鐨剉alue杈撳嚭鎴愪笅鎷夊垪琛ㄩ」鐨勫€箋€? 闄ゆ涔嬪锛岄〉闈笂闈㈣繕鏈変竴娈電畝鍗曠殑Javascript鑴氭湰锛屽畠浼氬湪鐢ㄦ埛鍦ㄩ€夋嫨涓嬫媺鍒楄〃涓煇涓€椤瑰悗锛屾彁浜ゅ寘鍚€渞eqeust_locale鈥濆彉閲忕殑琛ㄥ崟鍒癆ction銆? 涓婇潰椤甸潰鍜孞avaBean涓€鍏變嬌鐢ㄤ簡涓変釜鍥介檯鍖杒ey锛屾墍浠ラ渶瑕佸湪璧勬簮鏂囦歡瀹氫箟杩欎笁涓猭ey瀵瑰簲鐨勫浗闄呭寲娑堟伅銆傚洜涓烘湰绯葷粺浠呮敮鎸佺畝浣撲腑鏂囧拰缇庡紡鑻辮涓ょ鐜锛堝闇€瑕佸鍔犲叾浠栬瑷€涔熼潪甯哥畝鍗曪紝 鍙渶瑕佸鍔犳洿杩囩殑璧勬簮鏂囦歡锛屽苟绠€鍗曚慨鏀筁ocales绫誨嵆鍙級锛屾墍浠ラ渶瑕佸垎鍒湪涓枃璧勬簮鏂囦歡涓鍔犲涓嬩笁椤癸細
languag=閫夋嫨璇█
usen=缇庡紡鑻辮
zhcn=绠€浣撲腑鏂? 鍦ㄨ嫳鏂囪祫婧愭枃浠朵腑澧炲姞濡備笅涓夐」锛? languag=Select Lanuage
usen=American English
zhcn=Simplified Chinese
涓? 閫夋嫨绋嬪簭璇█
鏈簲鐢ㄤ負浜嗘洿濂界殑瀹夊叏鎬э紝灏嗘墍鏈夌殑JSP椤甸潰閮芥斁鍦╓EB-INF/jsp璺緞涓嬶紝浠庤€岄伩鍏嶄簡鐩存帴璁塊棶JSP椤甸潰锛屼負浜嗚鎵€鏈夌殑JSP椤甸潰閮借兘寰楀埌Struts2鐨勫鐞嗭紝鍦╯truts.xml鏂囦歡涓鍔犲涓嬮厤缃墖娈碉細
<!-- 浣跨敤閫氶厤绗﹀彿瀹氫箟Action鐨刵ame -->
<action name="*">
聽<!-- 灏嗚姹傝漿鍙戣WEB-INF/jsp璺緞涓嬪悓鍚嶇殑JSP椤甸潰 -->
<result>/WEB-INF/jsp/{1}.jsp</result>
</action>
濡傛灉鍦ㄦ祻瑙堝櫒涓姹俿electlanguage.action锛屽皢鐪嬪埌閫夋嫨绋嬪簭璇█鐨勯〉闈€? 濡傛灉鐢ㄦ埛閫氳繃涓婇潰鐨勪笅鎷夊垪琛ㄦ閫夋嫨浜嗏€滅編寮忚嫳璇€濋」鍚庯紝灏嗙湅鍒扮敤鎴烽€夋嫨缇庡紡鑻辮鐨勯〉闈€? 涓€鏃﹀畾涔変簡涓婇潰鐨勯〉闈㈠悗锛屾垜浠氨鍙互鍦↗SP椤甸潰涓€氳繃<s:include .../>鏍囩鏉ュ寘鍚椤甸潰锛屽寘鍚椤甸潰鍚庯紝灏卞彲浠ヨ嚜琛岄€夋嫨绋嬪簭璇█浜嗐€? 渚嬪鍦ㄧ櫥闄嗛〉闈腑閫氳繃濡備笅鏍囩鏉ュ寘鍚笂闈㈤€夋嫨绋嬪簭璇█鐨勯〉闈細
<!-- 鍖呭惈璁╃敤鎴瘋嚜琛岄€夋嫨绋嬪簭璇█鐨勯〉闈?-->
<s:include value="selectlanguage.jsp"/>
鍦ㄤ換浣曢〉闈腑澧炲姞浜嗕笂闈唬鐮佸悗锛岃椤甸潰灏辮兘鍏佽鐢ㄦ埛鑷閫夋嫨璇█銆傞€氬父锛屾垜浠彧闇€瑕佸湪搴旂敤鐨勭涓€涓〉闈㈣鐢ㄦ埛閫夋嫨绋嬪簭璇█锛屽悗缁〉闈㈢洿鎺ヤ嬌鐢ㄨ璇█鍗沖彲銆? 濡傛灉娴忚鑰呭湪娴忚鍣ㄧ殑鍦闆潃鏍忚闂甶nput.action锛孲truts2鑷姩杩涘叆WEB-INF/jsp/input.jsp椤甸潰锛屽皢鐪嬪埌鍏佽鐢ㄦ埛閫夋嫨绋嬪簭璇█鐨勯〉闈€? 濡傛灉鐢ㄦ埛甯屾湜浣跨敤缇庡紡鑻辮鐨勮瑷€锛屽垯鍦ㄥ厑璁哥敤鎴烽€夋嫨绋嬪簭璇█鐨勯〉闈㈢殑涓嬫媺鍒楄〃妗嗕腑閫変腑鈥滅編寮忚嫳璇€濋」锛屽皢鐪嬪埌浣跨敤缇庡紡鑻辮鐣岄潰鐨勯〉闈€? 濡傛灉鐢ㄦ埛鐧婚檰鎴愬姛锛屽皢鐪嬪埌缇庡紡鑻辮鐜涓嬬櫥闄嗘垚鍔熺殑椤甸潰銆傝椤甸潰鑷姩浣跨敤浜嗙編寮忚嫳璇殑鐜锛岃繖閮芥槸鍥犱負鐢ㄦ埛閫夋嫨浜嗙編寮忚嫳璇殑璇█鍚庯紝绯葷粺灏嗙敤鎴烽€夋嫨璁劇疆鎴愪簡Session鐨刉W_TRANS_I18N_LOCALE灞炴€у€鹼紝璇ession灞炴€у€肩洿鎺ュ喅瀹歋truts2绯葷粺鐨勮瑷€鐜銆?
聽
聽
聽
璧勬簮鏂囦歡鐨勫懡鍚嶏細basename_language_country.properties
JAVA鍥介檯鍖?
濡傛灉绯葷粺鍚屾椂瀛樺湪璧勬簮鏂囦歡銆佺被鏂囦歡锛岀郴缁熷皢浠ョ被鏂囦歡涓轟富锛岃€屼笉浼氳皟鐢ㄨ祫婧愭枃浠躲€?
瀵逛簬绠€浣撲腑鏂囩殑Locale锛孯esourceBundle鎼滅儲璧勬簮鐨勯『搴忔槸锛?
(1)baseName_zh_CN.class
(2)baseName_zh_CN.properties
(3)baseName_zh.class
(4)baseName_zh.properties
(5)baseName.class
(6)baseName.properties
聽
Struts2鐨勫浗闄呭寲鍏ラ棬
Struts2鍥介檯鍖栨槸寤虹珛鍦↗ava鍥介檯鍖栫殑鍩虹涓婄殑锛屼竴鏍鋒槸閫氳繃鎻愪緵涓嶅悓鍥藉/璇█鐜鐨勬秷鎭祫婧愶紝鐒跺悗閫氳繃ResourceBundle
鍔犺澆鎸囧畾Locale瀵瑰簲鐨勮祫婧愭枃浠訛紝鍐嶅彇寰楄璧勬簮鏂囦歡涓寚瀹歬ey瀵瑰簲鐨勬秷鎭?-鏁翠釜杩囩▼涓嶫AVA绋嬪簭鐨勫浗瀹跺寲瀹屽叏鐩稿悓锛屽彧鏄?
Struts2妗嗘灦瀵笿AVA绋嬪簭鍥介檯鍖栬繘琛屼簡杩涗竴姝ュ皝瑁咃紝浠庤€岀畝鍖栦簡搴旂敤绋嬪簭鐨勫浗闄呭寲銆?
Struts2闇€瑕佸浗闄呭寲鐨勯儴鍒?
绫誨瀷杞崲:
鏁版嵁鏍¢獙:
楠岃瘉妗嗘灦xml閰嶇疆鏂囦歡鐨勫浗闄呭寲锛歊egisterAction-validation.xml鏂囦歡<message key="username.xml.invalid"/>
JSP椤甸潰鐨勫浗闄呭寲锛?lt;s:text name="addUser"/>
Action鐨勫浗闄呭寲:鍒╃敤ActionSupport绫繪彁渚涚殑getText()鏂規硶.
Struts2涓姞杞藉叏灞€璧勬簮鏂囦歡
struts.xml
<constant name="struts.custom.i18n.resources" value="baseName"/>
鎴?
struts.properties
struts.custom.i18n.resources=baseName
璁塊棶鍥介檯鍖栨秷鎭?
Struts2璁塊棶鍥介檯鍖栨秷鎭富瑕佹湁濡備笅涓夌鏂瑰紡锛?
(1)JSP椤甸潰锛?lt;s:text name="key"/>
(2)Action绫諱腑锛氫嬌鐢ˋctionSupport绫葷殑getText鏂規硶銆?
(3)琛ㄥ崟鍏冪礌鐨凩abel閲岋細涓鴻〃鍗曞厓绱犳寚瀹氫竴涓猭ey灞炴€?
杈撳嚭甯﹀崰浣嶇鐨勫浗闄呭寲娑堟伅
Struts2涓彁渚涗簡濡備笅涓ょ鏂瑰紡鏉ュ~鍏呮秷鎭瓧绗︿覆涓殑鍗犱綅绗?
(1)JSP椤甸潰锛屽湪<s:text.../>鏍囩涓嬌鐢ㄥ涓?lt;s:param.../>鏍囩鏉ュ~鍏呮秷鎭腑鐨勫崰浣嶇銆?
(2)Action涓紝鍦ㄨ皟鐢╣etText鏂規硶鏃朵嬌鐢╣etText(String aTextName,List args)鎴杇etText(String key, String[] args)鏂規硶鏉ュ~鍏呭崰浣嶇銆?
闄ゆ涔嬪锛孲truts2杩樻彁渚涗簡瀵瑰崰浣嶇鐨勪竴绉嶆浛浠f柟寮忥紝杩欑鏂瑰紡鍏佽鍦ㄥ浗闄呭寲娑堟伅璧勬簮鏂囦歡涓嬌鐢ㄨ〃杈懼紡锛屽浜庤繖绉嶆柟寮忥紝鍒欏彲閬垮厤鍦ㄤ嬌鐢ㄥ浗闄呭寲娑堟伅鏃惰繕闇€瑕佷負鍗犱綅绗︿紶鍏ュ弬鏁闆€箋€?
濡備笅鍦ㄦ秷鎭祫婧愪腑浣跨敤琛ㄨ揪寮?
succTip=${username}, 娆㈣繋, 鎮ㄥ凡缁忕櫥褰?
鍦ㄤ笂闈㈢殑娑堟伅璧勬簮涓紝閫氳繃浣跨敤琛ㄨ揪寮忥紝鍙互浠嶸alueStack涓彇鍑鴻username灞炴€у€鹼紝鑷姩濉厖鍒拌娑堟伅璧勬簮涓€?
鍔犺澆璧勬簮鏂囦歡鐨勬柟寮?
(1)鍔犺澆鍏ㄥ眬璧勬簮鏂囦歡锛?<constant name="struts.custom.i18n.resources" value="baseName"/>
(2)鍖呰寖鍥磋祫婧愭枃浠?锛氫負Struts2鎸囧畾鍖呰寖鍥磋祫婧愭枃浠剁殑鏂規硶鏄?鍦ㄥ寘鐨勬牴璺緞涓嬪緩绔嬪涓枃浠跺悕涓簆ackage_language_country.properties鐨勬枃浠訛紝涓€鏃﹀緩绔嬩簡
聽聽聽 杩欎釜绯誨垪鐨勫浗闄呭寲璧勬簮鏂囦歡锛屽簲鐢ㄤ腑澶勪簬璇ュ寘涓嬬殑鎵€鏈堿ction閮藉彲浠ヨ闂璧勬簮鏂囦歡銆傞渶瑕佹敞鎰忕殑鏄笂闈㈢殑鍖呰寖鍥磋祫婧愭枃浠剁殑baseName灏辨槸package锛?
聽涓嶆槸Action鎵€鍦ㄧ殑鍖呭悕銆?
(3)Action鑼冨洿璧勬簮鏂囦歡锛氬湪Action绫繪枃浠舵墍鍦ㄧ殑璺緞寤虹珛澶氫釜鏂囦歡鍚嶄負ActionName_language_country.properties鐨勬枃浠躲€?
(4)涓存椂鎸囧畾璧勬簮鏂囦歡锛?lt;s:i18n.../>鏍囩鐨刵ame灞炴€ф寚瀹氫複鏃剁殑鍥介檯鍖栬祫婧愭枃浠?
鍔犺澆璧勬簮鏂囦歡鐨勯『搴?
Action涓姞杞借祫婧愭枃浠訛紝鍋囪鎴戜滑鍦ㄦ煇涓狢hildAction涓皟鐢ㄤ簡getText("user.title")锛孲truts 2.0鐨勫皢浼氭墽琛屼互涓嬬殑鎿嶄綔锛?
(1)浼樺厛鍔犺澆绯葷粺涓繚瀛樺湪ChildAction鐨勭被鏂囦歡鐩稿悓浣嶇疆锛屼笖baseName涓篊hildAction鐨勭郴鍒楄祫婧愭枃浠躲€?
(2)濡傛灉鍦?1)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝涓擟hildAction鏈夌埗绫籔arentAction锛屽垯鍔犺澆绯葷粺涓繚瀛樺湪ParentAction鐨勭被鏂囦歡鐩稿悓浣嶇疆锛屼笖
聽聽 baseName涓篜arentAction鐨勭郴鍒楄祫婧愭枃浠躲€?
(3)濡傛灉鍦?2)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝涓擟hildAction鏈夊疄鐜版帴鍙ChildAction锛屽垯鍔犺澆绯葷粺涓繚瀛樺湪IChildAction鐨勭被鏂囦歡鐩稿悓浣嶇疆锛屼笖
聽聽 baseName涓篒ChildAction鐨勭郴鍒楄祫婧愭枃浠躲€?
(4)濡傛灉鍦?3)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝涓擟hildAction鏈夊疄鐜版帴鍙odelDriven(鍗充嬌鐢ㄦā鍨嬮┍鍔ㄦā寮?锛屽垯瀵逛簬getModel()鏂規硶杩斿洖鐨刴odel瀵矽薄锛?
聽聽 閲嶆柊鎵ц绗?1)姝ユ搷浣溿€?
(5)濡傛灉鍦?4)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝鍒欐煡鎵懼綋鍓嶅寘涓媌aseName涓簆ackage鐨勭郴鍒楄祫婧愭枃浠躲€?
(6)濡傛灉鍦?5)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝鍒欐部鐫€褰撳墠鍖呬笂婧紝鐩村埌鏈€椤跺眰鍖呮潵鏌ユ壘baseName涓簆ackage鐨勭郴鍒楄祫婧愭枃浠躲€?
(7)濡傛灉鍦?6)涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝鍒欐煡鎵緎truts.custom.i18n.resources甯擱噺鎸囧畾baseName鐨勭郴鍒楄祫婧愭枃浠躲€?
(8)濡傛灉缁忚繃涓婇潰鐨勬楠や竴鐩存壘涓嶅埌key瀵瑰簲鐨勬秷鎭紝灏嗙洿鎺ヨ緭鍑鴻key鐨勫瓧绗︿覆鍊箋€?
瀵逛簬鍦↗SP涓闂浗闄呭寲娑堟伅锛屽垯绠€鍗曠殑澶氾紝浠栦滑鍙堝彲浠ュ垎涓轟袱绉嶅艦寮忥細
(1)瀵逛簬浣跨敤<s:i18n.../>鏍囩浣滀負鐖舵爣绛劇殑<s:text.../>鏍囩銆佽〃鍗曟爣绛劇殑褰㈠紡锛?
聽聽 a銆佸皢浠?lt;s:i18n.../>鏍囩鎸囧畾鐨勫浗闄呭寲璧勬簮鏂囦歡涓姞杞芥寚瀹歬ey瀵瑰簲鐨勬秷鎭€?
聽聽 b銆佸鏋滃湪a涓壘涓嶅埌鎸囧畾key瀵瑰簲鐨勬秷鎭紝鍒欐煡鎵緎truts.custom.i18n.resources甯擱噺鎸囧畾baseName鐨勭郴鍒楄祫婧愭枃浠躲€?
聽聽 c銆佸鏋滅粡杩囦笂闈㈡楠や竴鐩存壘涓嶅埌璇ey瀵瑰簲鐨勬秷鎭紝灏嗙洿鎺ヨ緭鍑鴻key鐨勫瓧绗︿覆鍊箋€?
(2)濡傛灉<s:text.../>鏍囩銆佽〃鍗曟爣绛炬病鏈変嬌鐢?lt;s:i18n.../>鏍囩浣滀負鐖舵爣绛撅細
聽聽聽 鐩存帴鍔犺澆struts.custom.i18n.resources甯擱噺鎸囧畾baseName鐨勭郴鍒楄祫婧愭枃浠躲€傚鏋滄壘涓嶅埌璇ey瀵瑰簲鐨勬秷鎭紝灏嗙洿鎺ヨ緭鍑鴻key鐨勫瓧绗︿覆鍊箋€?
聽
鍏佽鐢ㄦ埛鑷閫夋嫨绋嬪簭璇█
Struts2鍥介檯鍖栫殑杩愯鏈哄埗
聽聽聽 鍦⊿truts2涓紝鍙互閫氳繃ActionContext.getContext().setLocale(Locale arg)璁劇疆鐢ㄦ埛鐨勯粯璁よ瑷€銆?
涓轟簡绠€鍖栬缃敤鎴烽粯璁よ瑷€鐜锛孲truts2鎻愪緵浜嗕竴涓悕涓篿18n鐨勬嫤鎴櫒(Interceptor),骞朵笖灏嗗叾娉ㄥ唽鍦ㄩ粯璁ょ殑鎷︽埅鍣ㄤ腑(defaultStack)銆?
聽聽聽 i18n鎷︽埅鍣ㄥ湪鎵цAction鏂規硶鍓嶏紝鑷姩鏌ユ壘璇鋒眰涓竴涓悕涓簉equest_locale鐨勫弬鏁般€傚鏋滆鍙傛暟瀛樺湪锛屾嫤鎴櫒灏卞皢鍏朵綔涓哄弬鏁幫紝杞崲鎴怢ocale瀵矽薄锛?
骞跺皢鍏惰涓虹敤鎴烽粯璁ょ殑Locale(浠h〃鍥藉/璇█鐜)銆?
聽闄ゆ涔嬪锛宨18n鎷︽埅鍣ㄨ繕浼氬皢涓婇潰鐢熸垚鐨凩ocale瀵矽薄淇濆瓨鍦ㄧ敤鎴稴ession鐨勫悕涓篧W_TRANS_I18N_LOCALE鐨勫睘鎬т腑銆備竴鏃︾敤鎴稴ession涓瓨鍦ㄤ竴涓悕涓?
WW_TRANS_I18N_LOCALE鐨勫睘鎬э紝鍒欒灞炴€ф寚瀹氱殑Locale灏嗕細浣滀負娴忚鑰呯殑榛樿Locale銆?
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<script. type="text/javascript">
function langSelecter_onChanged()
{
聽document.getElementById("langForm").submit();
}
</script>
<%-- 璁劇疆SESSION_LOCALE涓虹敤鎴穝ession涓殑WW_TRANS_I18N_LOCALE灞炴€у€?--%>
<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>
<%-- 浣跨敤lee.Locales鍒涘緩locales瀹炰緥 --%>
<s:bean id="locales" name="lee.Locales">
聽<%-- 涓簂ocales瀹炰緥浼犲叆current鍙傛暟鍊鹼紝濡傛灉SESSION_LOCALE涓虹┖锛屽垯杩斿洖ValueStack涓璴ocale灞炴€у€?鍗崇敤鎴鋒祻瑙堝櫒璁劇疆鐨凩ocale) --%>
聽<s:param name="current" value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"/>
</s:bean>
<%-- 璁╃敤鎴烽€夋嫨璇█鐨勮〃鍗?--%>
<form. action="<s:url/>" id="langForm"
聽聽聽 style="background-color:#bbbbbb; padding-top: 4px; padding-bottom: 4px;">
聽聽聽 <s:text name="languag"/>
聽<s:select label="Language" list="#locales.locales" listKey="value" listValue="key"
聽聽聽聽聽聽聽 value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"
聽聽聽聽聽聽聽 name="request_locale" id="langSelecter"
聽聽聽聽聽聽聽 nchange="langSelecter_onChanged()" theme="simple"/>
</form>
鍦ㄥ叾浠栭〉闈腑鍖呭惈璇ラ〉闈?
<s:include value="selectlanguage.jsp"/>
鍦╯truts.xml鏂囦歡涓鍔燗ction閫氶厤绗︾殑閰嶇疆锛?
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
聽聽聽聽聽聽聽 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
聽聽聽聽聽聽聽 "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
聽<constant name="struts.custom.i18n.resources" value="messageResource"/>
聽<constant name="struts.i18n.encoding" value="GBK"/>
聽聽聽 <package name="lee" extends="struts-default">
聽聽<!-- 浣跨敤閫氶厤绗﹀畾涔堿ction鐨刵ame -->
聽聽<action name="*">
聽聽聽<!-- 灏嗚姹傝漿鍙戠粰/WEB-INF/jsp/璺緞涓嬪悓鍚嶇殑JSP椤甸潰 -->
聽聽聽<result>/WEB-INF/jsp/{1}.jsp</result>
聽聽</action>
聽聽聽 </package>
</struts>