I18nInterceptor À¹½ØÆ÷¼ò½é
¸ÃÀ¹½ØÆ÷´¦ÀídefaultStackµÚËĵÄλÖã¬ÊÇÓÃÀ´·½±ã¹ú¼Ê»¯µÄ£¬Èç¹û˵ÎÒÃǵÄÒ»¸öWebÏîÄ¿ÒªÖ§³Ö¹ú¼Ê»¯µÄ»°£¬Í¨³£µÄ×ö·¨ÊǸø¶¨Ò»¸öÏÂÀ¿òÁгöËùÖ§³ÖµÄÓïÑÔ£¬µ±Óû§Ñ¡ÔñÁËÒ»ÖÖÓïÑÔºóºóÃæä¯ÀÀµÄËùÓÐÒ³Ãæ×Ô¶¯Çл»µ½ËùÑ¡ÔñµÄÓïÑÔ°æ±¾£¬¶ø¸ÃÀ¹½ØÆ÷¾ÍÊÇÓÃÀ´ÊµÏָù¦Äܵġ£ÒªÊµÏÖÓïÑÔÇл»Ëµ°×Á˾ÍÊǶ¯Ì¬¸Ä±äLocale¡£
ÔÚ¸ÃÀ¹½ØÆ÷Öж¨ÒåÁËÈý¸öÊôÐÔ£¬·Ö±ðÊÇ£ºparameterName,requestOnlyParameterName,attributeName£¬ÕâÈý¸öÊôÐÔ¶¼ÓÐĬÈÏÖµ£¬ÏàÓ¦µØ·Ö±ðÊÇ£º"request_locale"£¬"request_only_locale","WW_TRANS_I18N_LOCALE"£¬µ±È»¿ÉÒÔÊÖ¶¯µØ¸ø¸ÃÀ¹½ØÆ÷´«µÝ²ÎÊýÒԸıäÕ⼸¸öÊôÐÔµÄÖµ¡£
µÚÒ»¸öÊôÐÔµÄÖ¸µÄÊÇÇл»Localeʱָ¶¨µÄrequest²ÎÊýÃû³Æ²¢ÇÒÒª°ÑLocale´æ´¢µ½sessionÖУ¬µÚ¶þ¸ö²ÎÊýÓëµÚÒ»¸öÒâ˼һÑùÖ»²»¹ý²»´æ´¢µ½sessionÖУ¬Ö»¶Ôµ±Ç°requestÓÐЧ£¬µÚÈý¸ö²ÎÊýÖ¸µÄÊÇ´æ´¢µ½sessionÖеÄLocaleµÄkey¡£ÏÂÃ濴һϸÃÀ¹½ØÆ÷ÊÇÈçºÎʵÏָù¦Äܵģº
±í1
@Override public String intercept(ActionInvocation invocation) throws Exception { //Ê¡ÂÔ... //»ñÈ¡ÇëÇó²ÎÊý Map<String, Object> params = invocation.getInvocationContext().getParameters();
boolean storeInSession = true;//ÊÇ·ñÒª°ÑLocale´æÔÚsessionÖеĿª¹Ø±äÁ¿ Object requested_locale = findLocaleParameter(params, parameterName); if (requested_locale == null) {//ÎÞrequest_locale²ÎÊý requested_locale = findLocaleParameter(params, requestOnlyParameterName); if (requested_locale != null) {//ÓÐrequest_only_locale²ÎÊý storeInSession = false;//ÊÇrequest_only_localeÔò²»´æ´¢ÔÚsessionÖÐ } }
//»ñÈ¡session Map Map<String, Object> session = invocation.getInvocationContext().getSession();
Locale locale = null; if (requested_locale != null) {//ÇëÇó²ÎÊýÖÐÓÐrequest_locale»òrequest_only_locale //½«×Ö·û´®×ª»¯ÎªLocale¶ÔÏó locale = (requested_locale instanceof Locale) ? (Locale) requested_locale : LocalizedTextUtil.localeFromString(requested_locale.toString(), null); //Ê¡ÂÔ... } if (session != null) { synchronized (session) { if (locale == null) {//Èç¹ûÇëÇó²ÎÊýÖм´Ã»ÓÐrequest_localeҲûÓÐrequest_only_locale // check session for saved locale Object sessionLocale = session.get(attributeName);//Èç¹ûsessionÖÐÒѾÓÐLocale¶ÔÏó if (sessionLocale != null && sessionLocale instanceof Locale) { locale = (Locale) sessionLocale;//°ÑsessionÖеÄLocale¶ÔÏó»ñÈ¡³öÀ´ //Ê¡ÂÔ... } else {//Èç¹ûsessionÖÐҲûÓÐLocale¶ÔÏó // no overriding locale definition found, stay with current invokation (=browser) locale locale = invocation.getInvocationContext().getLocale();//Ôò»ñÈ¡ActionContextÖеÄLocale //Ê¡ÂÔ... storeInSession = false;//²¢ÇÒ²»´æ´¢µ½sessionÖÐ } } if (storeInSession) {//Èç¹ûstoreInSessionΪtrueÔò°ÑLocale´æ´¢µ½SessionÖÐ session.put(attributeName, locale); } } } saveLocale(invocation, locale);//½«Locale´æ´¢µ½ActionContextÖÐ //Ê¡ÂÔ... return result; } |
±í2
//´ÓÇëÇó²ÎÊýÖлñÈ¡Locale×Ö·û´®Öµ private Object findLocaleParameter( Map<String, Object> params, String parameterName ) { Object requested_locale = params.remove(parameterName); if (requested_locale != null && requested_locale.getClass().isArray() && ((Object[]) requested_locale).length == 1) { requested_locale = ((Object[]) requested_locale)[0]; } return requested_locale; } |
±í3
//½«Locale¶ÔÏó´æ´¢µ½ActionContextÖÐ protected voidsaveLocale(ActionInvocation invocation, Locale locale) { invocation.getInvocationContext().setLocale(locale); } |
ΪÁ˸ü¼¯ÖÐÓÚ¹¦ÄÜʵÏÖ£¬ÉÏÃæµÄ·½·¨ÖÐÊ¡ÂÔÁËÈÕÖ¾ifÅжϣ¬Èç¹ûÒª¿´ÍêÕûÔ´ÂëÇë²Î¿´struts2×Ô´øÔ´Âë¡£
sturts2ÊǸù¾ÝActionContextÖеÄLocale¶ÔÏóÈ¥ÕÒÏàÓ¦µÄ¹ú¼Ê»¯×ÊÔ´ÎļþµÄ£¬ËùÒÔÒª¶¯Ì¬Çл»ÓïÑÔ°æ±¾¾ÍÊÇÒª¶¯Ì¬¸Ä±äActionContextÖеÄLocale¶ÔÏó£¬intercept·½·¨ÖеÄÖ´ÐÐÂß¼ÉÏÃæµÄ×¢ÊÍÒѾдµÃÇå³þÁË£¬ÕâÀïÁоÙһϴ«µÝrequest²ÎÊýʱ¿ÉÄܳöÏÖµÄÇé¿ö£º
1. Ö»´«µÝrequest_locale²ÎÊý£¬¸ÃÀ¹½ØÆ÷¾Í»á½«¸Ã²ÎÊýֵת³ÉÒ»¸öLocale¶ÔÏó£¬È»ºóÒÔ"WW_TRANS_I18N_LOCALE"ΪÎü´æ´¢µ½sessionÖУ¬²¢ÇÒ¸üÐÂActionContextÖеÄLocale¶ÔÏó£¬ÕâÑùÈç¹ûÔÚÒÔºóµÄÇëÇó²ÎûÓд«µÝrequest_locale²ÎÊý£¬ÕâʱsessionÖÐÒѾÓÐÁËLocale¶ÔÏó£¬ÄÇô¸ÃÀ¹½ØÆ÷¾Í»á½«sessionÖеÄLocale¶ÔÏó¸üе½ActionContextÖУ¬ÕâÑù¾Í¿ÉÒÔʵÏÖÓïÑÔµÄÇл»ÁË¡£
2. Ö»´«µÝrequest_only_locale¶ÔÊý£¬ÕâÖÖÇé¿öÏÂLocale¶ÔÏó²»»á´æ´¢µ½sessionÖУ¬ÔڸôÎÇëÇóÄܽ«´«µÝµÄLocale¸üе½ActionContextÖÐÒòΪûÓд洢µ½sessionÖУ¬ÔÚÒÔºóµÄÇëÇóÖÐÈç¹ûûÓд«µÝrequest_only_locale»òrequest_locale£¬ÄÇô±»¸üе½ActionContextÖÐLocale£¬»¹ÊÇActionContextÔÏȵÄLocale£¬ÕâÑùÓֻص½ÁËÔÀ´µÄÓïÑÔ°æ±¾¡£
3. request_localeÓërequest_only_locale¶¼Ã»Óд«µÝ£¬ÕâÖÖÇé¿ö²»ÓýâÊÍ¡£