天天看点

Tiny Dict框架

使用了Tiny的Dict框架,现象如下,本地测试没问题,放到服务器上之后,报空指针,指示没有获取到指定的字典。

远程调试代码,发现是DictManagerImpl在查找指定的字典时没有找到对应的dictLoader,DictManagerImpl获取字典的代码如下:

public Dict getDict(String dictTypeName, Context context) {
        String lang = LocaleUtil.getContext().getLocale().toString();
        return getDict(lang, dictTypeName, context);
//      if(!dictLoaderMap.containsKey(lang)){
//          throw new RuntimeException("Locale:{}" + lang + "涓嶅瓨鍦ㄥ搴旂殑DictLoader");
//      }
//      for (DictLoader dictLoader : dictLoaderMap.get(lang)) {
//          Dict dict = dictLoader.getDict(dictTypeName, this, context);
//          if (dict != null) {
//              return dict;
//          }
//      }
//      throw new RuntimeException("娌℃湁鎵惧埌<" + dictTypeName + ">鐨勫瓧鍏哥被鍨�);
    }

    public Dict getDict(String lang, String dictTypeName, Context context) {
        if (!dictLoaderMap.containsKey(lang)) {
            throw new DictRuntimeException(DictExceptionErrorCode.DICT_LOADER_NOT_FOUND, lang);
//          throw new RuntimeException("Locale:{}" + lang + "涓嶅瓨鍦ㄥ搴旂殑DictLoader");
        }
        for (DictLoader dictLoader : dictLoaderMap.get(lang)) {
            Dict dict = dictLoader.getDict(dictTypeName, this, context);
            if (dict != null) {
                return dict;
            }
        }
        throw new DictRuntimeException(DictExceptionErrorCode.DICT_TYPE_NAME_NOT_FOUND, dictTypeName);
//      throw new RuntimeException("娌℃湁鎵惧埌<" + dictTypeName + ">鐨勫瓧鍏哥被鍨�);
    }      

经过与本地的调试的对比,发现获取本地语言的代码:

String lang = LocaleUtil.getContext().getLocale().toString();      

在本地运行时,获取到的是zh_CN,而在服务器运行时获取到的是en_US。本地的操作系统是中文的,远程服务器是英文linux,于是将远程服务器的本地语言改成中文,问题解决了。

疑问:

DictManager管理DictLoader,其持有了一个Map<String, List> dictLoaderMap类型的域dictLoaderMap。

dictLoaderMap中的key在put和get时都是LocaleUtil.getContext().getLocale().toString()

为什么在英文环境下会出现获取不到DictLoader的现象?

<tiny-filter id="setLocaleTinyFilter" class="setLocaleTinyFilter">
      <filter-mapping url-pattern=".*" />
      <init-param name="defaultLocale" value="zh_CN" />
      <init-param name="defaultCharset" value="UTF-8" />
      <init-param name="inputCharset" value="_input_charset" />
      <init-param name="outputCharset" value="_output_charset" />
      <init-param name="paramKey" value="_lang" />
      <init-param name="sessionKey" value="_lang" />
    </tiny-filter>