Java瑣事

          -I am not alone
          posts - 54, comments - 30, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
              系統中需要統計用戶的數量及動態,所以我通過listener來監聽用戶的登錄退出(session的變化),但如何跟分層的ssh整合起來呢?下面詳細說下:
          1. 首先新建listener, 我這里繼承了HttpSessionListener和ServletContextListener。并通過這個方法獲取bean
          1public 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>
          這樣好像就可以了
          主站蜘蛛池模板: 香格里拉县| 四平市| 麻阳| 北安市| 壤塘县| 开江县| 武平县| 武清区| 新安县| 马关县| 五常市| 潜江市| 鹤山市| 凤凰县| 铜梁县| 九龙坡区| 宾川县| 伊宁市| 江川县| 镇坪县| 张掖市| 合阳县| 白水县| 兴国县| 万荣县| 那坡县| 胶南市| 元氏县| 罗定市| 翁牛特旗| 称多县| 平度市| 玉门市| 青田县| 民勤县| 绥芬河市| 中卫市| 乐东| 衡水市| 东山县| 六安市|