【问题标题】:How to create global session/application session to store application options using spring?如何使用spring创建全局会话/应用程序会话来存储应用程序选项?
【发布时间】:2015-05-23 16:25:40
【问题描述】:

如何使用spring创建全局会话/应用程序会话来存储应用程序选项?

我正在开发 Web 应用程序,我想在服务器启动时从数据库中检索徽标、站点名称、站点 URL 等全局变量。

比的最佳实践是什么,我如何从 jsp 调用这个变量?

【问题讨论】:

    标签: java session spring-mvc web


    【解决方案1】:

    您需要使用应用程序上下文;

    public class SomeSpringBean {
      @Autowired
      private WebApplicationContext appContext;
    
      public void storeAndRetrieveSomethingInAppScope() {
         appContext.getServletContext().setAttribute(String name, Object object);
         Object fromAppContext = appContext.getServletContext().getAttribute(String name);
      }
    }
    

    【讨论】:

      【解决方案2】:

      在 Java 代码中,您可以创建会话变量:

      request.getSession().setAttribute("site_name", "Example");
      

      然后在jsp文件中检索它:

      ${sessionScope.site_name}
      

      【讨论】:

        猜你喜欢
        • 2021-06-06
        • 2013-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-26
        • 2016-06-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多