posts - 66, comments - 12, trackbacks - 0, articles - 0

          jsp中使用spring aop (轉帖)

          Posted on 2008-08-05 11:11 cyantide 閱讀(703) 評論(0)  編輯  收藏 所屬分類: spring

          在JSP中使用Spring其實很容易,主要用到Spring的WebApplicationContextUtils.getWebApplicationContext函數。

          要再JSP里面得到ApplicationContext需要這么做,首先import="org.springframework.web.context.support.*,org.springframework.context.*"

          然后可以通過如何做法:

          ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

          這樣就得到了ApplicationContext,就可以操作Spring了。

          JSP本來就可以認為是一個Servlet,所以使用getServletContext()就是理所應當了。

          一:web.xml配置

          <servlet>
          <servlet-name>context</servlet-name>
          <servlet-class>
          org.springframework.web.context.ContextLoaderServlet
          </servlet-class>
          <load-on-startup>1</load-on-startup>
          </servlet>
          <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/applicationContext.xml</param-value>
          </context-param>

           


          二:在JSP

          <%@ page import="org.springframework.context.ApplicationContext"%>
          <%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
          <%@ page import="com.yourcompany.service.CategoryService"%>

          <%
          //上面的CategoryService引用是我自己的東西
          //applicationContext.xml中一定要有完整的依賴鏈,從dataSource到CategoryService

          ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
          CategoryService cs = (CategoryService) ctx.getBean("CategoryService");
          List list =cs.getCategoryDAO().findAll();
          %>


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 湖州市| 綦江县| 东城区| 静乐县| 定安县| 阜新| 阿拉善右旗| 天台县| 镇康县| 靖西县| 阳江市| 乐都县| 汝城县| 漳州市| 延寿县| 高淳县| 二连浩特市| 剑河县| 衢州市| 台东县| 屏东县| 东阿县| 虎林市| 乌兰察布市| 廊坊市| 云龙县| 浮梁县| 水城县| 彭山县| 特克斯县| 宜章县| 株洲市| 德江县| 铜梁县| 扶沟县| 什邡市| 南江县| 通海县| 曲靖市| 泽州县| 雷山县|