【问题标题】:How to add some Spring attributes when the application starts应用启动时如何添加一些Spring属性
【发布时间】:2011-06-30 03:52:14
【问题描述】:

我是 Spring 框架的新手,我正在开发一个必须是多语言的 Web 应用程序。

转换后的值在数据库中,我不希望每次渲染视图时都从数据库中选择值。

我们的想法是从应用程序的某个地方获取它们。所以,我想在我的应用程序启动时加载这些值,但我不知道它们的确切位置和放置位置。

感谢您的帮助!

【问题讨论】:

    标签: spring spring-mvc internationalization


    【解决方案1】:

    解决这个问题的常用方法是为您的消息创建一个 .properties 文件,每种语言都有一个,或者让框架决定使用浏览器的语言环境,或者直接指定它。

    请参阅参考 here 以了解 Spring MVC 中的语言环境支持以及两个不同的示例 herehere

    如果您坚持为您的 i18n 数据使用数据库,当然也可以,但据我所知,没有内置支持。请参阅this 线程以获取想法。

    【讨论】:

      【解决方案2】:

      我找到了解决方案。我在春天像这样创建了一个 bean:

      public class DictionaryAccessor implements ServletContextAware, InitializingBean{
      
          @Resource
          private DictionaryServices dictionaryServices;
      
          protected ServletContext context;
          @Override
          public void setServletContext(ServletContext servletContext) {
              context = servletContext;
          }
      
          @Override
          public void afterPropertiesSet() throws Exception {
              logger.debug("initializing dictionary");
              loadDictionary();
              logger.debug("done with dictionary");
          }
      

      DictionaryAccessor 将在我的应用程序启动时加载。在loadDictionary 方法中,我将我的值作为属性放在ServletContext 对象中。

      在我的 jsps 中,我创建了自己的标签并访问了翻译,或者在 .zul 文件中我从我的 tld 访问了一个函数。

      【讨论】:

        猜你喜欢
        • 2019-12-08
        • 1970-01-01
        • 1970-01-01
        • 2016-01-13
        • 2021-10-13
        • 1970-01-01
        • 1970-01-01
        • 2017-10-11
        • 2020-10-04
        相关资源
        最近更新 更多