在网上搜索了很久,看到对index的数据初始化一般是连接数据库使用一个jsp形式对数据进行输出,或者使用jsp的forward标签,虽然前段的url不改变但是后台经过一次跳转相应的出发了一个action,有的是通过在web.xml配置对应的action进行跳转比较麻烦,还有就是struts2中的action标签不能够自动刷新本页面,只有在通过点击触发之后才能刷新本页面而不跳转,从一篇博文中看到在web.xml中使用监听器进行初始化,就是通过配置
<listener>
<listener-class>com.yousun.init.InitDate</listener-class>
</listener>
实现webcontext的初始化,通过重写ServletContextListener类中的contextInitialized方法可以把全局共享数据放到webContext中示例:
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
ServletContext context = arg0.getServletContext();
context.setAttribute("user", "游隼!!!");
将数据保存在了application中,可以再index界面通过
<s:property value="#application.user"/>获取内容
通过这种方法完成了SSH中首页面数据的初始化工作。
如果需要获取spring的sessionFactory可以使用
WebApplicationContext springContext=WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
并通过 springContenxt的getBean("sessionFactory")获取sessionFactory对象。实现从数据库获取数据,还可以通过Timer定时器操作对首页面中的数据进行定时更新