將程序從一個容器換到另一個容器,總會有各種意料之外的困難需要解決,近日本人需要將一個Web工程從Tomcat環境轉移到WebSphere環境,經歷了一番周折,特地將此經過記錄下來,也許它能對將要進行如此經歷的人其一點幫助作用,另外在此也向網絡同仁和工作中的同事表示感謝。
原環境:
程序:SSH
容器:Tomcat6.0
數據庫:MySql5
新環境:
程序:SSH
容器:WebSphere6.1
數據庫:Oracle10g
移植過程中的第一個困難,是WebSphere不認識Web.xml中的Struts taglib.原文字(適用于Tomcat)如下:
<!-- Struts的TLDS -->
<taglib>
<taglib-uri>/WEB-INF/tld/app.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/app.tld</taglib-location>
</taglib>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/tld/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
這個問題因為之前有所準備,在網絡上找到了答案,將上述文字包在<jsp-config>節點中即可,修改后(對Tomcat和WebSphere均適用)的文字如下:
<!-- Struts的TLDS -->
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/tld/app.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/app.tld</taglib-location>
</taglib>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/tld/struts-bean.tld</taglib-uri>
<taglib-location>
/WEB-INF/tld/struts-bean.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-html.tld</taglib-uri>
<taglib-location>
/WEB-INF/tld/struts-html.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-logic.tld</taglib-uri>
<taglib-location>
/WEB-INF/tld/struts-logic.tld
</taglib-location>
</taglib>
</jsp-config>
如此處理后,首頁顯示出來了,隨即翻頁遇到了問題,在IE中是翻頁出現404錯誤,在FF中好一點,它告訴我WebSphere無法解析struts配置文件struts-config.xml。
起初我以為是中文問題,刪除struts-config.xml中所有中文注釋問題依舊,接下來在網絡中尋找,還真有和我遇到一樣問題的難友,但沒人提出解決方案,正在撓頭之際,我們的PM忽然說是否JDK不一致,檢查一下,本機用的是1.6,而WebSphere自帶1.5的,將本機也調成1.5后,問題解決! 真是只有咒語能解開咒語。
再下來,在表單提交時遇到了亂碼問題,這是因為之前聽信網絡意見,將Web.xml中的filter都去掉了,結果自然亂碼。此時感覺網絡傳聞未必可信,于是將filter又重新加上,亂碼沒有了。看來不經親自嘗試而盲從網絡傳聞是要吃虧的。
再下來,程序要訪問數據庫了,于是在WebSphere6.1中設置了數據源,再在Spring配置文件中進行了設置,如下:
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"
value="java:comp/env/jdbc/*******DS">
</property>
</bean>
這樣寫在Tomcat中好用,在WebSphere不好用,正在撓頭之際,PM告我別的項目有同樣的寫法,于是一看,java:comp/env/這部分是不需要的,直接把數據源JNDI名寫在Value中就可以了。
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"
value="jdbc/*******DS">
</property>
</bean>
其它數據庫移植的問題就交給了Hibernate。至此問題全部解決。
事后來看,WebSphere6.1對中文,SSH的支持還是很好的,只是有些特定的地方和傳統的Tomcat中的項目不太一樣,注意一下就好了,未必有想象中的困難。遇到困難時,向有同樣經歷的人請教比自己在網絡上搜尋要快很多。