開發環境:1、MyEclipse 6.5
2、Tomcat 6.0
3、Struts2.0 + Hibernate3.2 + Spring2.0
一、編碼前的操作
1、創建項目。在MyEclipse中新建一個Web Project,名爲mytest,使用的是JavaEE 5 Library。項目實際地址爲G:\workspace\ssh\mytest
2、將項目部署到tomcat。進入tomcat目錄下的conf中(G:\apache-tomcat-6.0.30\conf),打開server.xml,在</Host>之上添加一行代碼
<Context path="/mytest" docBase="G:\workspace\ssh\mytest\WebRoot" reloadable="true" />
添加到項目的映射
3、將tomcat與MyEclipse關聯。在MyEclipse中,Window->Preferences->MyEclipse Enterprise Workbench->Servers->Tomcat->Tomcat 6.x,設定好相關Tomcat的目錄。設定好目錄後可以把MyEclipse自帶的Tomcat禁用掉。在MyEclipse中,Window->Preferences->MyEclipse Enterprise Workbench->Servers->Integrated Sandbox->Myeclipse Tomcat6,點擊disabled.
4、使用MyEclipse自帶的導入功能來整合。步驟如後幾點。
5、選中項目mytest,在MyEclipse中,myeclipse->Project Capabilities->Add Hibernate Capabilities,在彈出的對話框中選中Hibernate 3.2,"JAR Library Installation"選擇"Copy checked Library ....." 。如果不選copy的話,部署到Tomcat時會有class not found 。
6、一步步next,當到選擇database connection時,因爲我們的小項目採用spring來管理,所以不選"Specify database connection detail?",同理,也不選"Create SessionFactory calss?"。一直到最後,就增加了項目的Hibernate支持。
7、接下來增加項目對Spring的支持。選中項目mytest,在MyEclipse中,myeclipse->Project Capabilities->Add Spring Capabilities,在彈出的對話框中選中Spring 2.0,選擇前4個Library和Spring2.0 web library,,"JAR Library Installation"選擇"Copy checked Library ....." 。
8、在接下來的彈窗中,不選"Enable AOP builder",將applicationContext.xml指定放在WEB-INF目錄下,不選"Create Spring SessionFactory that reference"。一直到最後,就增加了項目的Spring支持。
9、手動添加對Structs的支持。使用的是structs 2.0.11。將(commons-logging-1.0.4.jar這個包spring帶了,所以不用拷貝)freemarker-2.3.8.jar,ognl-2.6.11.jar,structs2-core-2.0.11.jar,structs2-spring-plugin-2.0.11.jar,xwork-2.0.4.jar拷貝到WEB-INF/lib/下。
10、在src目錄下新建structs.xml文件,輸入
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
</struts>
項目採用的是Spring來管理,相當於action只是在structs中申明。
11、在WEB-INF/web.xml添加structs的過濾器。在<welcome-file-list>之上添加
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
這樣就添加了structs的支持。
12、在WEB-INF/web.xml中添加Spring的監聽器。在</web-app>之上添加
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
做完這個之後就可以啓動tomcat看看,應該沒有任何問題。
到現在爲止,整個項目已經添加好了ssh的支持了。