隨筆 - 1  文章 - 37  trackbacks - 0
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          留言簿(16)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          test

          搜索

          •  

          最新評論

          關(guān)于本文
          本文記錄了將Spring Web Application應(yīng)用到OSGI環(huán)境中,也就是OSGI Web應(yīng)用。
          [請點擊這里下載本文的Eclipse工程]
          org.phrancol.osgi.demo.launcher工程中啟動應(yīng)用。

          一、準備工作

          1,JDK 1.5
          2,Eclipse 3.3-jee (MyEclipse)
          3,Spring-framework-2.1-m3
          4,Spring-osgi-1.0-m2

          二、搭建環(huán)境

          1,首先當然需要一個Http Server,那就用Eclipse自帶的Jetty吧。
          2,將Jetty和它的依賴包作為 Plugin 工程導(dǎo)入Eclipse,  Import -> Plug-ins and fragments -> Next

           3,創(chuàng)建一個普通工程做為Eclipse Launch的存放目錄 org.phrancol.osgi.demo.launcher
           4,Open Run Dialog ... -> OSGI Framework -> New Configuration ,名字改為org.phrancol.osgi.demo.web.launcher,在
          Common面板Save as 里面的 Shared file指定目錄到 org.phrancol.osgi.demo.launcher

          5,Apply,  Run ,可以看到一個Launch已經(jīng)生成,通過Console可以看到Jetty也成功啟動
          6,在OSGI輸入exit退出,(注:如果不退出,那么它將駐留內(nèi)存,大概會消耗20K的內(nèi)存)

          三、Equinox/Spring-osgi環(huán)境

          導(dǎo)入Spring-osgi和它的依賴包(注:導(dǎo)入Spring包的時候,plug-in Location 要指定Spring-osgi的lib目錄),導(dǎo)入完畢后,形成的project結(jié)構(gòu)如下圖


          四、jpetstore
          使用Spring自帶的jpetstore,代碼和配置文件都是現(xiàn)成的,能省去一些不必要的開發(fā)。

          五、首頁
          jpetstore默認是springmvc,向HttpService注冊Servlet或是Resource,jpetstore里面需要注冊的servlet就是ContextLoaderServlet和DispatcherServlet,resource則是view層的jsp等。
          1,創(chuàng)建個plug-in project ,OSGI Framework選擇Equinox,創(chuàng)建Activator,org.phrancol.osgi.jpetstore.springmvc
          2,創(chuàng)建一個web目錄,這個目錄就是web應(yīng)用的目錄,將jpetstore里面的WEB-INF/jsp目錄拷貝進去
          3,在 META-INF 目錄中創(chuàng)建 dispatcher 目錄,將petstore-servlet.xml拷貝進去,修改成如下代碼
          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

          <!--
            - DispatcherServlet application context for the Spring web MVC
            - implementation of JPetStore's web tier.
            
          -->
          <beans>

              
          <!-- ========================= VIEW DEFINITIONS ========================= -->

              
          <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  
          <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                  
          <property name="prefix" value="/web/jsp/spring/"/>
                  
          <property name="suffix" value=".jsp"/>
              
          </bean>

              
          <!-- ========================= DEFINITIONS OF PUBLIC CONTROLLERS ========================= -->

              
          <bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

              
          <bean name="/shop/index.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
                  
          <property name="viewName" value="index"/>
              
          </bean>

          </beans>

          4,在Activator的start(BundleContext context)里面注冊servlet和resource,代碼如下
          public void start(BundleContext context) throws Exception {
                  ServiceReference sr 
          = context.getServiceReference(HttpService.class
                          .getName());
                  HttpService httpService 
          = (HttpService) context.getService(sr);
                  httpService.registerResources(
          "/""/web"null);
                  httpService.registerServlet(
          "/*.jsp"new JspServlet(context
                          .getBundle(), 
          "/web/jsp"), nullnull);
                  Dictionary
          <String, String> initparams = new Hashtable<String, String>();
                  initparams.put(
          "load-on-startup""1");
                  DispatcherServlet dispatcherServlet 
          = new DispatcherServlet();
                  dispatcherServlet
                          .setContextConfigLocation(
          "META-INF/dispatcher/petstore-servlet.xml");

                  initparams 
          = new Hashtable<String, String>();
                  initparams.put(
          "servlet-name""petstore");
                  initparams.put(
          "load-on-startup""2");
                  httpService.registerServlet(
          "/*.do", dispatcherServlet, initparams,
                          
          null);

              }

          注意依賴包關(guān)系
          啟動后,在訪問http://localhost/shop/index.do頁面,報錯,看來要解決一下標簽庫問題,在web目錄中建立一個WEB-INF目錄,將c.tld, fmt.tld拷貝進去,然后在osgi控制臺refresh springmvc,再訪問這個頁面,發(fā)現(xiàn)沒有圖片,把jpetstore/images目錄拷貝到web目錄下,再refresh一下,OK,首頁出來了。

          六、結(jié)束語
           這部分簡單記錄了如何使用springmvc,下部分將記錄如何將jpetstore的各個層通過spring-osgi應(yīng)用起來

          posted on 2007-09-06 14:09 Phrancol Yang 閱讀(6290) 評論(9)  編輯  收藏 所屬分類: OSGI

          FeedBack:
          # re: Developing Equinox/Spring-osgi/Spring Framework Web Application Part 1 - 顯示首頁 2007-09-08 12:45 Friszart
          寫得很清晰!  回復(fù)  更多評論
            
          # re: Developing Equinox/Spring-osgi/Spring Framework Web Application Part 1 - 顯示首頁 2007-10-31 10:25 fangzx
          謝謝作者,寫的非常清楚,條理分明。準備照做一下。

          下載本文的Eclipse工程: 下載不了!  回復(fù)  更多評論
            
          # re: Developing Equinox/Spring-osgi/Spring Framework Web Application Part 1 - 顯示首頁 2007-10-31 21:31 Phrancol Yang
          可以下載啊,我剛剛才試的,這個blog提供的空間太小,所以就放到免費的網(wǎng)絡(luò)硬盤上了,可能有些不穩(wěn)定,多試一下  回復(fù)  更多評論
            
          # re: Developing Equinox/Spring-osgi/Spring Framework Web Application Part 1 - 顯示首頁 2007-10-31 21:40 Fangzx
          Phrancol Yang,謝謝回復(fù)。不過,還是無法下載。

          1、我發(fā)現(xiàn)了類似的Blog文章:
          http://thegoodthebadtheugly.wordpress.com/2007/05/20/springosgi/7
          希望本文作者能多提供技術(shù)支持,整個過程問題太多了,比較痛苦。

          2、我按照本文步驟做了一下,不過我用的是 :
          spring-framework-2.5-rc1
          spring-osgi-1.0-m3
          最后遇到問題,找不到下面的插件:
          javax.servlet.jsp.jstl.fmt
          org.apache.taglibs.standard.tag.common.fmt
          不知道到哪里下載?

          3、發(fā)現(xiàn) spring-osgi-1.0-m3中好像少了很多插件,所以只能重新下載spring-osgi-1.0-m2,從其中提取了很多插件。

          4、我的ss內(nèi)容:

          id State Bundle
          0 ACTIVE org.eclipse.osgi_3.3.1.R33x_v20070828
          1 INSTALLED org.springframework.bundle.spring.webmvc_2.5.0.rc1
          2 ACTIVE org.springframework.osgi.spring-osgi-extender_1.0.0.m3
          3 ACTIVE org.mortbay.jetty_5.1.11.v200706111724
          4 ACTIVE org.eclipse.osgi.services_3.1.200.v20070605
          5 ACTIVE org.eclipse.equinox.http.servlet_1.0.1.R33x_v20070816
          6 ACTIVE org.eclipse.equinox.http.jetty_1.0.1.R33x_v20070816
          7 INSTALLED org.springframework.osgi.jstl.osgi_1.1.2.SNAPSHOT
          8 ACTIVE org.apache.commons.logging_1.0.4.v200706111724
          9 ACTIVE org.springframework.osgi.spring-osgi-io_1.0.0.m3
          10 ACTIVE javax.servlet.jsp_2.0.0.v200706191603
          11 ACTIVE javax.servlet_2.4.0.v200706111738
          12 ACTIVE org.eclipse.equinox.jsp.jasper_1.0.1.R33x_v20070816
          13 ACTIVE org.springframework.osgi.aopalliance.osgi_1.0.0.SNAPSHOT
          14 ACTIVE org.springframework.bundle.spring.core_2.5.0.rc1
          15 ACTIVE org.springframework.bundle.spring.context_2.5.0.rc1
          16 ACTIVE org.springframework.osgi.spring-osgi-core_1.0.0.m3
          17 ACTIVE org.springframework.osgi.cglib-nodep.osgi_2.1.3.SNAPSHOT
          18 ACTIVE org.springframework.bundle.spring.web_2.5.0.rc1
          19 ACTIVE org.springframework.bundle.spring.beans_2.5.0.rc1
          20 ACTIVE org.springframework.bundle.spring.aop_2.5.0.rc1
          21 ACTIVE org.apache.commons.el_1.0.0.v200706111724
          22 INSTALLED org.phrancol.osgi.jpetstore.springmvc_1.0.0
          23 ACTIVE org.apache.jasper_5.5.17.v200706111724

            回復(fù)  更多評論
            
          # re: Developing Equinox/Spring-osgi/Spring Framework Web Application Part 1 - 顯示首頁 2007-10-31 22:53 Fangzx
          @Phrancol Yang
          非常感謝你的回復(fù)。用IE可以下載了(原來用FF)。
          我會加你的QQ,期望得到你的更多的指教。  回復(fù)  更多評論
            
          # re: Developing Equinox/Spring-osgi/Spring Framework Web Application Part 1 - 顯示首頁 2007-11-19 10:58 gembin
          請點擊這里下載本文的Eclipse工程]

          無法下載+!

          如何下載?

          Gembin@gmail.com  回復(fù)  更多評論
            
          # re: Developing Equinox/Spring-osgi/Spring Framework Web Application Part 1 - 顯示首頁 2007-11-19 15:50 Phrancol
          免費網(wǎng)絡(luò)硬盤,你在“立即提取文件”欄目中點一下‘提取’,會進入下載頁面  回復(fù)  更多評論
            
          # re: Developing Equinox/Spring-osgi/Spring Framework Web Application Part 1 - 顯示首頁 2008-02-13 10:23 delphixp
          @Phrancol

          點擊"提取" 按鈕, 沒任何反應(yīng)....  回復(fù)  更多評論
            
          # re: Developing Equinox/Spring-osgi/Spring Framework Web Application Part 1 - 顯示首頁 2008-02-14 16:25 Phrancol Yang
          用IE瀏覽器  回復(fù)  更多評論
            
          主站蜘蛛池模板: 广德县| 拉萨市| 且末县| 容城县| 繁峙县| 正镶白旗| 元阳县| 招远市| 武定县| 翼城县| 镇江市| 崇阳县| 临夏县| 乾安县| 微山县| 获嘉县| 孝感市| 许昌县| 韶山市| 阜阳市| 南雄市| 绩溪县| 松阳县| 凌源市| 临西县| 安龙县| 荣成市| 乌鲁木齐县| 繁峙县| 独山县| 克东县| 禄丰县| 内乡县| 饶阳县| 象州县| 广河县| 宣化县| 依兰县| 新建县| 荣成市| 醴陵市|