搭建一個(gè)Struts+Spring+Hibernate的web框架
Posted on 2006-10-25 13:36 Kevin Meng 閱讀(951) 評(píng)論(0) 編輯 收藏開(kāi)發(fā)環(huán)境:
window xp
jdk 1.5
tomcat 5.028
eclispe 3.2
myeclipse 4.0
步驟:
(1)新建一個(gè)web project,名稱(chēng)為suzhouadmin
(2)在項(xiàng)目上點(diǎn)鼠標(biāo)右鍵,選擇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,然后點(diǎn)Next
(4)folder改為:WebRoot/WEB-INF,F(xiàn)ile為applicationContext.xml不變。點(diǎn)Finish。
Spring的引用完成。
(5)在項(xiàng)目上點(diǎn)鼠標(biāo)右鍵,選擇myeclipse->add Struts capablities...
(6)選擇struts1.1,base package for classes?改成 com.suzhou.admin.struts,然后點(diǎn)finish。
Struts的引用完成。
(6)新建立一個(gè)包c(diǎn)om.suzhou.admin.hibernate,然后在項(xiàng)目上點(diǎn)鼠標(biāo)右鍵,選擇myeclipse->add Hibernate capablities...
(7)把Hibernate 3.0 core lib,Hibernate 3.0 advanced lib中,Copy Checked lib content to project folder,點(diǎn)Next
(8)選擇Spring Configuration file(applicationContext.xml),點(diǎn)Next
(9)選擇Exit spring configuration file.輸入sessionFactory id為sessionFactory。點(diǎn)Next
(10)設(shè)置數(shù)據(jù)源ID為dataSource,選擇一個(gè)已經(jīng)設(shè)置好的DB profile,然后點(diǎn)Next
(11)點(diǎn)Package...選擇com.suzhou.admin.hibernate,自動(dòng)生成類(lèi)名com.suzhou.admin.hibernate.HibernateSessionFactory。
開(kāi)始配置Struts和Spring.
(12)打開(kāi)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就配置好了。通過(guò)以上配置,Web容器會(huì)自動(dòng)加載/WEB-INF/applicationContext.xml初始化
ApplicationContext實(shí)例,如果需要指定配置文件位置,可通過(guò)context-param加以指定:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
配置完成之后,即可通過(guò)
WebApplicationContextUtils.getWebApplicationContext()
方法在Web應(yīng)用中獲取ApplicationContext引用。
(13)通過(guò)Struts config新建一個(gè)Action名為L(zhǎng)oginAction,自動(dòng)生成的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打開(kāi)applicationContext.xml,點(diǎn)右鍵,選擇new Bean,輸入Bean name為/login,calss為com.suzhou.admin.struts.action.LoginAction。點(diǎn)finish,applicationContext.xml多加了一個(gè)Bean定義:
<bean name="/login" class="com.suzhou.admin.struts.action.LoginAction" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"></bean>
window xp
jdk 1.5
tomcat 5.028
eclispe 3.2
myeclipse 4.0
步驟:
(1)新建一個(gè)web project,名稱(chēng)為suzhouadmin
(2)在項(xiàng)目上點(diǎn)鼠標(biāo)右鍵,選擇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,然后點(diǎn)Next
(4)folder改為:WebRoot/WEB-INF,F(xiàn)ile為applicationContext.xml不變。點(diǎn)Finish。
Spring的引用完成。
(5)在項(xiàng)目上點(diǎn)鼠標(biāo)右鍵,選擇myeclipse->add Struts capablities...
(6)選擇struts1.1,base package for classes?改成 com.suzhou.admin.struts,然后點(diǎn)finish。
Struts的引用完成。
(6)新建立一個(gè)包c(diǎn)om.suzhou.admin.hibernate,然后在項(xiàng)目上點(diǎn)鼠標(biāo)右鍵,選擇myeclipse->add Hibernate capablities...
(7)把Hibernate 3.0 core lib,Hibernate 3.0 advanced lib中,Copy Checked lib content to project folder,點(diǎn)Next
(8)選擇Spring Configuration file(applicationContext.xml),點(diǎn)Next
(9)選擇Exit spring configuration file.輸入sessionFactory id為sessionFactory。點(diǎn)Next
(10)設(shè)置數(shù)據(jù)源ID為dataSource,選擇一個(gè)已經(jīng)設(shè)置好的DB profile,然后點(diǎn)Next
(11)點(diǎn)Package...選擇com.suzhou.admin.hibernate,自動(dòng)生成類(lèi)名com.suzhou.admin.hibernate.HibernateSessionFactory。
開(kāi)始配置Struts和Spring.
(12)打開(kāi)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就配置好了。通過(guò)以上配置,Web容器會(huì)自動(dòng)加載/WEB-INF/applicationContext.xml初始化
ApplicationContext實(shí)例,如果需要指定配置文件位置,可通過(guò)context-param加以指定:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
配置完成之后,即可通過(guò)
WebApplicationContextUtils.getWebApplicationContext()
方法在Web應(yīng)用中獲取ApplicationContext引用。
(13)通過(guò)Struts config新建一個(gè)Action名為L(zhǎng)oginAction,自動(dòng)生成的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打開(kāi)applicationContext.xml,點(diǎn)右鍵,選擇new Bean,輸入Bean name為/login,calss為com.suzhou.admin.struts.action.LoginAction。點(diǎn)finish,applicationContext.xml多加了一個(gè)Bean定義:
<bean name="/login" class="com.suzhou.admin.struts.action.LoginAction" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"></bean>