Posted on 2010-11-21 16:44
石子路口 閱讀(683)
評論(0) 編輯 收藏 所屬分類:
網絡教學資源平臺
系統中需要統計用戶的數量及動態,所以我通過listener來監聽用戶的登錄退出(session的變化),但如何跟分層的ssh整合起來呢?下面詳細說下:
1. 首先新建listener, 我這里繼承了HttpSessionListener和ServletContextListener。并通過這個方法獲取bean
1
public Object getBean(String name)
2
{
3
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
4
return ctx.getBean(name);
5
}
2. 在web.xml中配置路徑
<listener>
<listener-class>cn.edu.ujn.wsjx.listener.LoginUserListener</listener-class>
</listener>
這里注意與spring的listener可能會有先后順序
在listener里可以 ILoginlService loginService = (ILoginlService)getBean("loginService"); 來使用loginService了
另外一個比較常用的servlet也可以類似的配置來獲取spring的bean,來更好的有分層效果
目的:servlet與spring整合
1. 寫出這個類,可以繼承HttpServlet或者其他的servlet類,通過下面的語句獲得voteService
IVoteService voteService = (IVoteService)WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
獲得voteService后,就能和action一樣調用service層中的方法了
2. 在web.xml中配置<servlet></servlet>和<servlet-mapping></servlet-mapping>
這樣好像就可以了