在该拦截器中定义了三个属性,分别是:parameterName,requestOnlyParameterName,attributeName,这三个属性都有默认值,相应地分别是:"request_locale","request_only_locale","WW_TRANS_I18N_LOCALE",当然可以手动地给该拦截器传递参数以改变这几个属性的值。第一个属性的指的是切换Locale时指定的request参数名称并且要把Locale存储到session中,第二个参数与第一个意思一样只不过不存储到session中,只对当前request有效,第三个参数指的是存储到session中的Locale的key。下面看一下该拦截器是如何实现该功能的:
@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; }
//从请求参数中获取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; }
//将Locale对象存储到ActionContext中 protected void saveLocale(ActionInvocation invocation, Locale locale) { invocation.getInvocationContext().setLocale(locale); }