Posted on 2006-10-25 13:36
Kevin Meng 閱讀(951)
評論(0) 編輯 收藏
開發環境:
window xp
jdk 1.5
tomcat 5.028
eclispe 3.2
myeclipse 4.0
步驟:
(1)新建一個web project,名稱為suzhouadmin
(2)在項目上點鼠標右鍵,選擇myeclipse->add spring capablities...
(3)把Spring 1.2 AOP,Spring 1.2 Core Lib,Spring ORM/DAO/Hibernate 3.0 lib,Spring 1.2 web lib選擇中,Copy Checked lib content to project folder,然后點Next
(4)folder改為:WebRoot/WEB-INF,File為applicationContext.xml不變。點Finish。
Spring的引用完成。
(5)在項目上點鼠標右鍵,選擇myeclipse->add Struts capablities...
(6)選擇struts1.1,base package for classes?改成 com.suzhou.admin.struts,然后點finish。
Struts的引用完成。
(6)新建立一個包com.suzhou.admin.hibernate,然后在項目上點鼠標右鍵,選擇myeclipse->add Hibernate capablities...
(7)把Hibernate 3.0 core lib,Hibernate 3.0 advanced lib中,Copy Checked lib content to project folder,點Next
(8)選擇Spring Configuration file(applicationContext.xml),點Next
(9)選擇Exit spring configuration file.輸入sessionFactory id為sessionFactory。點Next
(10)設置數據源ID為dataSource,選擇一個已經設置好的DB profile,然后點Next
(11)點Package...選擇com.suzhou.admin.hibernate,自動生成類名com.suzhou.admin.hibernate.HibernateSessionFactory。
開始配置Struts和Spring.
(12)打開WEB-INFO/web.xml,在最后面添加:
?<servlet>
??<servlet-name>context</servlet-name>
??<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
??<load-on-startup>1</load-on-startup>
?</servlet>
這樣,Spring 的ApplicationContext就配置好了。通過以上配置,Web容器會自動加載/WEB-INF/applicationContext.xml初始化
ApplicationContext實例,如果需要指定配置文件位置,可通過context-param加以指定:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
配置完成之后,即可通過
WebApplicationContextUtils.getWebApplicationContext()
方法在Web應用中獲取ApplicationContext引用。
(13)通過Struts config新建一個Action名為LoginAction,自動生成的action定義如下:
??? <action
????? attribute="loginForm"
????? input="/index.jsp"
????? name="loginForm"
????? path="/login"
????? scope="request"
????? type="com.suzhou.admin.struts.action.LoginAction">
????? <forward name="loginfail" path="/error.jsp" />
????? <forward name="loginok" path="/main.jsp" />
??? </action>
把其改成:
??? <action
????? attribute="loginForm"
????? input="/index.jsp"
????? name="loginForm"
????? path="/login"
????? scope="request"
????? type="org.springframework.web.struts.DelegatingActionProxy">
????? <forward name="loginfail" path="/error.jsp" />
????? <forward name="loginok" path="/main.jsp" />
??? </action>
并在struts-config.xml中添加Spring插件。
?<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
??<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
?</plug-in>
(13)用Myeclipse Spring donfig editor打開applicationContext.xml,點右鍵,選擇new Bean,輸入Bean name為/login,calss為com.suzhou.admin.struts.action.LoginAction。點finish,applicationContext.xml多加了一個Bean定義:
<bean name="/login" class="com.suzhou.admin.struts.action.LoginAction" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"></bean>