新聞中心
1. 項目需要有Struts包和Spring的core, aop, web 三個包(注意不是Spring自己的Web MVC), 將Spring整合Strus。具體的 .classpath 文件如下所示:

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設,博望企業(yè)網(wǎng)站建設,博望品牌網(wǎng)站建設,網(wǎng)站定制,博望網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,博望網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
- ﹤?xml version="1.0" encoding="UTF-8"?﹥
- ﹤classpath﹥
- ﹤classpathentry kind="src" path="src"/﹥
- ﹤classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/﹥
- ﹤classpathentry kind="con" path="com.genuitec.core.J2EE14_CONTAINER"/﹥
- ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/antlr.jar"/﹥
- ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/beanutils.jar"/﹥
- ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/digester.jar"/﹥
- ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/fileupload.jar"/﹥
- ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/logging.jar"/﹥
- ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/validator.jar"/﹥
- ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jakarta-oro.jar"/﹥
- ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts.jar"/﹥
- ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java--bin.jar"/﹥
- ﹤classpathentry kind="con" path="melibrary.com.MYECLIPSE_SPRING20_CORE"/﹥
- ﹤classpathentry kind="con" path="melibrary.com.MYECLIPSE_SPRING20_AOP"/﹥
- ﹤classpathentry kind="con" path="melibrary.com.MYECLIPSE_SPRING20_WEB"/﹥
- ﹤classpathentry kind="output" path="WebRoot/WEB-INF/classes"/﹥
- ﹤/classpath﹥
2. 對Struts 配置文件做修改加入Spring 托管功能.
創(chuàng)建 Spring 配置文件,將文件放到src 目錄下,文件名稱為 applicationContext.xml, 編譯后放到WEB-INF/classes/ 下.
配置struts-config.xml文件,添加 spring的插件, 位置在 struts-config 文件的最末尾.
- value="/WEB-INF/classes/applicationContext.xml" />
- plug-in>
3. 修改 Struts 的 struts-config.xml 中的Action配置。原來的代碼:
- attribute="loginForm"
- input="/login.jsp"
- name="loginForm"
- path="/login"
- scope="request"
- validate="true"
- type="com.test.struts.action.LoginAction" />
改變后的代碼:
- attribute="loginForm"
- input="/login.jsp"
- name="loginForm"
- path="/login"
- scope="request"
- validate="true"
- type="org.springframework.web.struts.DelegatingActionProxy" />
type 部份為修改內容, 這里將使用spring的代理器來對Action進行控制. 當提交到/login.do是將控制權交給了spring,然后由spring來把它轉回到struts的Action.
4. Spring整合Strus,需要配置spring來實例化上一步中被刪除的 Action 類.
- xml version="1.0" encoding="UTF-8"?>
- >
bean> - beans>
Spring 通過 org.springframework.web.struts.DelegatingActionProxy 這個類, 然后根據(jù) Struts 配置文件中的 和 Spring 配置文件中的 來將 Spring 管理下的 Struts Action 類和提交的路徑匹配起來, 這些就是關于轉交控制權的配置內容.
實踐的過程中發(fā)現(xiàn)必須把 singleton="false"去掉才行, 否則就會無法初始化 Spring 框架, 不知道具體的原因是什么.
既然這個 Struts的Action 已經通過 Spring 來初始化, 所以就可以加入依賴注入, 整合Hibernate 的功能了. 例如典型的情況:
- com.test.struts.action.LoginAction
- private UserManager userManager;
- public UserManager getUserManager()...
- public void setUserManager(UserManager userMgmr) ...
- public ActionForward execute(ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- System.out.println("userManager=" + getUserManager());
- getUserManager().someBusinessMethods();
- .....
- }
然后就可以配置:
同理 Spring整合Hibernate 和沒Struts 的時候一樣, 也可以在這個文件中進行配置即可.
5. Spring整合Strus最后一步, 測試, 只要能打印出來 userManager 不為空, 就說明整合成功了. 如果出現(xiàn) 404 action servlet 不可用的錯誤, 一般都是 Spring 配置文件出錯導致的.
新聞名稱:五步搞定Spring整合Strus
當前路徑:http://fisionsoft.com.cn/article/cdiieej.html


咨詢
建站咨詢
