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