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

          jsp中使用spring aop (轉帖)

          Posted on 2008-08-05 11:11 cyantide 閱讀(709) 評論(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();
          %>


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


          網站導航:
           
          主站蜘蛛池模板: 封丘县| 保靖县| 北票市| 新泰市| 高青县| 淄博市| 宜章县| 阳原县| 长泰县| 明溪县| 荥阳市| 汝南县| 夏邑县| 郎溪县| 胶南市| 启东市| 沛县| 花莲市| 嘉义县| 双桥区| 通城县| 东山县| 锡林浩特市| 营口市| 宁陕县| 金堂县| 永泰县| 察隅县| 宝鸡市| 依安县| 卓尼县| 兴城市| 海南省| 龙岩市| 安康市| 轮台县| 饶河县| 宝清县| 溆浦县| 惠州市| 色达县|