jojo's blog--快樂憂傷都與你同在
          為夢想而來,為自由而生。 性情若水,風起水興,風息水止,故時而激蕩,時又清平……
          posts - 11,  comments - 30,  trackbacks - 0
          http://tech.blogbus.com/logs/219144.html

          兩個月前,寫了一篇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 2008-10-05 23:25 Blog of JoJo 閱讀(232) 評論(0)  編輯  收藏 所屬分類: Programming 相關

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章分類

          文章檔案

          新聞分類

          新聞檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 禹州市| 日土县| 大余县| 平乡县| 旬邑县| 昌乐县| 桃园县| 凤庆县| 卢湾区| 会同县| 九寨沟县| 绥芬河市| 南召县| 西昌市| 乌恰县| 康保县| 新源县| 五莲县| 台前县| 宜城市| 咸丰县| 永春县| 宝山区| 潜山县| 故城县| 西贡区| 双桥区| 柘荣县| 本溪市| 富蕴县| 廉江市| 北宁市| 开平市| 兴隆县| 炉霍县| 扎鲁特旗| 湘潭市| 克拉玛依市| 阳高县| 天长市| 青岛市|