如何在启动应用程序时从表中的json文件中加载数据?

问题描述 投票:-1回答:1

我在.js文件中有一个json对象。我需要在应用程序开始时从表中加载该对象中的数据。对象格式是这样的: -

               {'0':'Statement1',
                '1':'statement2',
                '2':'statement3',
                 .
                 .
                 . 
                 so on
                };
both index and statement column of that table need to populate data in this object at the start or load of the application. Please suggest. Thanks in advance.
javascript json database servlets struts
1个回答
0
投票

您可以使用ServletContextListener实现在服务器启动期间执行或加载静态数据,甚至可以在服务器停止后销毁。

public class YourContextListner
       implements ServletContextListener{

@Override
public void contextDestroyed(ServletContextEvent arg0) {
//write the code to destroy the object 
}

@Override
public void contextInitialized(ServletContextEvent arg0) {
//write the code to load your JSON data as Key value 
}
}

并且您必须在web.xml中创建一个条目来执行contextListner的映射

<listener>
<listener-class>
  com.yourpackageName.YourContextListner
</listener-class>

您甚至可以使用JSON解析器API来解析这些数据并存储到Key值对中。希望这有助于谢谢

© www.soinside.com 2019 - 2024. All rights reserved.