我们的应用程序使用Struts2 Internalization和Spring AbstractRoutingDataSource
进行动态数据库更改。更改语言,应选择相应的语言数据库。
因此,下面的代码使用Spring RoutingDataSource
进行动态数据库选择。
public class RoutingDataSource extends AbstractRoutingDataSource {
protected Object determineCurrentLookupKey() {
return LanguageContextHolder.getLanguagetype();
}
}
Spring RoutingDataSource
工作正常。
我的问题是如何在更改时设置区域设置。由于它存储在Struts2中,我无法在Spring中访问它。
我发现Spring MVC org.springframework.context.i18n.LocaleContextHolder
会这样做,但由于我们使用的是Struts2,我该怎么办呢?
答案 0 :(得分:0)
Struts2使用默认密钥WW_TRANS_I18N_LOCALE
在会话中存储当前区域设置。要在RoutingDataSource
使用ActionContext.getContext().getSession()
方法中访问会话。
// ...
if (ActionContext.getContext() != null) {
Map<String, Object> session = ActionContext.getContext().getSession();
if (session != null && session.containsKey(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)) {
Locale locale = (Locale) session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE);
}
}
// ...
答案 1 :(得分:0)
在上面的示例中,“return LanguageContextHolder.getLanguagetype();”将返回ThreadLocal类型。它实现如下。
public class LanguageContextHolder implements Serializable {
private static ThreadLocal<LanguageType> languageType = new ThreadLocal<LanguageType>();
public static ThreadLocal<LanguageType> getLanguagetype() {
return languageType;
}
public static void setLanguagetype(ThreadLocal<LanguageType> languagetype) {
languageType = languagetype;
}
public static void clearLanguageType() {
languageType.remove();
}
}