1. 為項目添加Struts能力
a) 右鍵點擊項目,選擇Build Path->Add Extendal Archives
, 選擇struts2安裝包中的lib檔案夾下如下JAR包(版本可以不同,前
綴相同的即可.圖中為2.1,實際項目為2.2)
b) 點擊完成,添加後的項目多了一個web.xml中添加了核心過濾器
的配置。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecut
eFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
c)在src檔案夾下,建立一個名為Struts.xml的檔案(用於配置action
):可以直接拷貝Struts 2.2.2.1安裝包中的Struts.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>
<package name="default" namespace="/" extends="struts-
default">
<default-action-ref name="index" />
<action name="index">
<result>
/index.jsp
</result>
</action>
</package>
</struts>
2. 為項目添加Spring能力
a) 右鍵點擊項目,選擇MyEclipse->Add Spring
Capabilities,或者點擊功能表列中MyEclipse->Project Capabilities
->Add Spring Capabilities。
b) 選擇Spring3.0版本,類庫選擇 Spring 3.0 AOP、Spring
3.0 Core、Spring 3.0 Persistence Core、Spring 3.0 Persistence
JDBC、Spring 3.0 Web 5個庫,並勾選複製到本地WEB-INF/lib目錄。
c) 點擊下一步,預設配置不需修改。
d) 點擊完成,查看添加Spring後的項目,增加了
applicationContex.xml檔案
3. 為項目添加Hibernate能力
a) 右鍵點擊項目,選擇MyEclipse->Add Hibernate
Capabilities,或者點擊功能表列中MyEclipse->Project Capabilities
->Add Hibernate Capabilities。
b) 選擇Hibernate3.3版本,如果要使用註記可選中啟用
annotation,但是會對後續自動產生實體類造成麻煩,不選就是用xml
檔案形式進行實體類映射,預設勾選Hibernate 3.3 Annotation &
Entity Manager、Hibernate 3.3 Core Libraries兩個類庫,複製jar
檔案到本地WEB-INF/lib檔案夾下。
c) 點擊下一步,選擇使用Spring的設定檔
applicationContext.xml,這樣就不會產生hibernate.cfg.xml,產生
與否看個人習慣。
d) 點擊下一步,選擇已經存在的applicationContext.xml檔案
的位置以及建立sessionFactory的名字
e) 下一步,選擇已經建立好的資料庫連接sshtest001
f) 點擊下一步,取消勾選,不產生HibernateSessionFactory
類
g) 點擊完成,查看工程,applicationContext.xml檔案中增加
了資料庫連接的配置,包括datasource和sessionFactory
4.測試專案:
右鍵項目,-->Run As -->MyEclipse Server Application
首頁項目“This is my JSP page. ”即為架構配置成功。