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();
          %>


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


          網站導航:
           
          主站蜘蛛池模板: 巴彦县| 崇明县| 千阳县| 全南县| 平陆县| 舒兰市| 凤阳县| 新郑市| 金门县| 宁阳县| 天津市| 华亭县| 台前县| 商都县| 越西县| 宁波市| 玉溪市| 舒兰市| 榆中县| 涡阳县| 类乌齐县| 突泉县| 寿宁县| 卢氏县| 涪陵区| 保亭| 平邑县| 天等县| 康乐县| 金昌市| 岫岩| 聂拉木县| 襄汾县| 客服| 马边| 拉萨市| 蕉岭县| 昆山市| 广西| 合山市| 法库县|