兩個月前,寫了一篇blog,名為《將Tapestry整合到Spring里去》,是根據(jù)文檔做了理論上的說明。這陣子終于開始動手做,由于犯了一個很低級的錯誤,浪費了很多時間,直到周六才“擺平”,很高興。網(wǎng)上這方面的資料非常少,我把實際操作過程再介紹一下,也算補一下文檔的不足。事實上Spring網(wǎng)站的文檔是給對Spring和Tapestry都有開發(fā)經(jīng)驗的人寫的,多少有點過于簡略,不是很方便使用。
第一步:寫一個Java Bean供后邊調用:
package my;//接口
public interface IBean {
public void amethod();
}
package my;//實現(xiàn)類
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);//你可以看看這一句在什么時候執(zhí)行,從而了解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>
小結:事實上,這個過程與我上次寫的內容大體相似,不過經(jīng)過實踐之后,我可以肯定地說這是走得通的,如果你配置時出現(xiàn)問題,那可能是在一些其它地方忽略了什么。象我這一次就是……嗯,具體的不說了,說出來會很不好意思的。如果有人配置時正好看到這篇文檔,希望對你有幫助^_^
Ps其實這一次失誤的過程也可以寫成篇blog,重點在于期間的心路歷程。不過還是算了。這段時間的工作受情緒影響太大了,如果以后成為管理者,一定要注意下屬的情緒問題并盡量予以解決,而不能認為職場不需要關注這樣的事。
又:Blogbus回退時輸入數(shù)據(jù)會丟失,雖然我知道這是Web應用開發(fā)里的難題,不過還是希望能夠列入待解決的問題里去-_-