Dict.CN 在線詞典, 英語學習, 在線翻譯

          都市淘沙者

          荔枝FM Everyone can be host

          導航

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          公告


          我的blog中的部分資源是來自于網絡上,如果你認為侵犯了你的權利,請及時聯系jelver#163.com,我會盡快刪除!另外如果要留言最好通過郵件的形式跟我交流因為我不是很經常寫文章,前面有的朋友留言好久了我才發現,所以對不住大家,現在留下MSN:jelver#163.com和QQ:253840881,歡迎交流! (用@代替#)

          隨筆分類

          文章分類

          隨筆檔案

          文章檔案

          相冊

          統計

          留言簿(23)

          積分與排名

          優秀學習網站

          友情連接

          閱讀排行榜

          評論排行榜

          Tapestry整合Spring實踐

          兩個月前,寫了一篇blog,名為《將Tapestry整合到Spring里去》,是根據文檔做了理論上的說明。這陣子終于開始動手做,由于犯了一個很低級的錯誤,浪費了很多時間,直到周六才“擺平”,很高興。網上這方面的資料非常少,我把實際操作過程再介紹一下,也算補一下文檔的不足。事實上Spring網站的文檔是給對Spring和Tapestry都有開發經驗的人寫的,多少有點過于簡略,不是很方便使用。

          第一步:寫一個Java Bean供后邊調用:

          package my;//接口
          public interface IBean {
           public void amethod();
          }

          package my;//實現類
          public class Bean implements IBean {
           
           public void amethod() {
            //do something;
           }
          }

          第二步:編寫Spring的context config文檔applicationContext.xml,放在web應用的/WEB-INF目錄下

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
          "
          http://www.springframework.org/dtd/spring-beans.dtd">
          <beans>
           <bean id="aBean" class="my.Bean">
           </bean>
          </beans>

          第三步:先試試直接載入Spring Application Context的做法。寫一個tapestry頁面(這里要求你對tapestry多少有點認識),如test.html,在對應的test.java中寫入以下代碼:

          WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(getRequestCycle().getRequestContext().getServlet().getServletContext());
          IBean bean = appContext.getBean("aBean");
          bean.amethod();

          這里要說明一下,Spring的幫助文檔有點錯誤,它用了WebApplicationContextUtils.getApplicationContext()的方法,但實際不存在這個方法,應該用WebApplicationContextUtils.getWebApplicationContext()

          第四步,改為推薦的做法,在Engine里做上邊的getWebApplicationContext(),并將其寫到tapestry的global對象里邊去。

          public class MyEngine extends BaseEngine {
           public static final String APPLICATION_CONTEXT_KEY = "appContext";
           protected void setupForRequest(RequestContext context) {
            super.setupForRequest(context);
            Map global = (Map) getGlobal();
            ApplicationContext ac = (ApplicationContext) global.get(APPLICATION_CONTEXT_KEY);
            if (ac == null) { ac = WebApplicationContextUtils.getWebApplicationContext(context.getServlet().getServletContext());
             System.out.println("測試" + ac);//你可以看看這一句在什么時候執行,從而了解Engine是什么時候被調用的;
             global.put(APPLICATION_CONTEXT_KEY, ac);
            }
           }
          }

          global是一個Map,我們在里邊增加了一個屬性appContext,并將取到的ApplicationContext放進去,以后,通過調用global.appContext就可以得到context,并用來getBean。

          第五步,在tapestry應用的sample.application文檔(文件名與web.xml對應)里,指定所使用的Engine,也就是第四步寫的內容:

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE application PUBLIC
            "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
            "
          http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">

          <application name="Sample Application" 
           engine-class="MyEngine">
           <page name="test" specification-path="test.page"/>
          </application>

          第六步:在test.html對應的test.page里,加入獲取bean的操作,代碼如下:

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE page-specification PUBLIC
            "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
            "
          http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
          <page-specification class="test">
            <property-specification name="aBean" type="my.IBean">
             global.appContext.getBean("aBean")
            </property-specification>
          </page-specification>

          第七步:在test.java里增加一個abstract getter:

          public abstract IBean getABean();

          之后,就可以在代碼里用getABean()的方法獲取我們需要的bean了,如:

          getABean().amethod();

          當然,不要忘了編輯web.xml文檔:

          <?xml version="1.0"?>
          <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "
          http://java.sun.com/dtd/web-app_2_3.dtd">
          <web-app>
           <display-name>Tapestry Tutorial</display-name>
           <!--Redirect it to the servlet mapping address /h-->
           <filter>
            <filter-name>redirect</filter-name>
            <filter-class>org.apache.tapestry.RedirectFilter</filter-class>
            <init-param>
             <param-name>redirect-path</param-name>
             <param-value>/app</param-value>
            </init-param>
           </filter>

           <filter-mapping>
            <filter-name>redirect</filter-name>
            <url-pattern>/</url-pattern>
           </filter-mapping>
             
           <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
           </context-param>
           <servlet>
            <servlet-name>context</servlet-name>
            <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
           </servlet>
           
           <servlet>
            <servlet-name>sample</servlet-name>
            <servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
            <load-on-startup>0</load-on-startup>
           </servlet>

           <servlet-mapping>
            <servlet-name>sample</servlet-name>
            <url-pattern>/app</url-pattern>
           </servlet-mapping>

           <session-config>
            <session-timeout>15</session-timeout>
           </session-config>
           
           <welcome-file-list>
            <welcome-file>index.htm</welcome-file>
           </welcome-file-list>
           
          </web-app>

          小結:事實上,這個過程與我上次寫的內容大體相似,不過經過實踐之后,我可以肯定地說這是走得通的,如果你配置時出現問題,那可能是在一些其它地方忽略了什么。象我這一次就是……嗯,具體的不說了,說出來會很不好意思的。如果有人配置時正好看到這篇文檔,希望對你有幫助^_^

          Ps其實這一次失誤的過程也可以寫成篇blog,重點在于期間的心路歷程。不過還是算了。這段時間的工作受情緒影響太大了,如果以后成為管理者,一定要注意下屬的情緒問題并盡量予以解決,而不能認為職場不需要關注這樣的事。

          又:Blogbus回退時輸入數據會丟失,雖然我知道這是Web應用開發里的難題,不過還是希望能夠列入待解決的問題里去-_-
           

          posted on 2006-02-26 10:42 都市淘沙者 閱讀(720) 評論(4)  編輯  收藏

          評論

          # 有個問題 2006-07-03 10:05 tapestryaa

          為什么在web.xml中用
          <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>

          啟動時報錯 error Listenerstart   回復  更多評論   

          # re: Tapestry整合Spring實踐 2006-07-03 12:42 jelver

          看看你的項目,是不是缺少必須的包呢  回復  更多評論   

          # re: Tapestry整合Spring實踐 2006-07-04 09:12 tapestryaa

          我用myeclipse做的,在做之前都要倒包,myeclipse自己倒  回復  更多評論   

          # re: Tapestry整合Spring實踐 2006-12-18 17:02 程程

          包也導了。還是不對。。請問還有可能是什么錯誤呢。給點提示吧。
          謝謝
            回復  更多評論   


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


          網站導航:
           
          主站蜘蛛池模板: 兴和县| 玉林市| 庐江县| 灵山县| 上饶县| 吉林省| 屯昌县| 隆子县| 宜都市| 潜江市| 兴文县| 吉林省| 宁武县| 于都县| 新建县| 平和县| 绩溪县| 吉林省| 邢台市| 永仁县| 尼玛县| 聊城市| 句容市| 离岛区| 阳西县| 湖州市| 余庆县| 樟树市| 会泽县| 讷河市| 盐亭县| 高淳县| 怀化市| 兰西县| 南乐县| 洛浦县| 新河县| 垦利县| 宁津县| 清新县| 鹿邑县|