基於Maven的S2SH(Struts2+Spring+Hibernate)框架搭建

1. 前言

基於Maven的開發方式開發項目已經成爲主流。Maven能很好的對項目的層次及依賴關係進行管理。方便的解決大型項目中複雜的依賴關係。S2SH(Struts2+Spring+Hibernate)框架是目前進行WEB項目開發中非常流行的一個組合。基於作者對於這個三個框架的瞭解及其相關的開發經驗,介紹下搭建基於Maven的S2SH框架的過程。

2. Maven頂級父倉庫(Repository)工程

如果要從頭到尾搭建Maven工程的話,這步是必不可少的。創建頂級Maven倉庫成功的方法有很多,這裏只介紹一個最簡單的。

(1) 創建一個文件夾,名稱作爲這個工程的名字。在此文件夾下面創建一個pom.xml文件。這個文件的內容爲:

複製代碼
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.example.S2SH</groupId>

<artifactId>S2SH</artifactId>

<version>1.0.0</version>

<packaging>pom</packaging>

</project>
複製代碼

 

其中,紅色部分是必不可少的。它確定了這個POM文件的三維座標。

(2) 添加其他描述性的信息。

這部分主要是爲了描述這個系統的功能,對應的項目主頁,其中的開發人員等信息。這部分的內容略去,有興趣的可以去網上搜索相關的信息。

(3) 在Eclipse中添加Maven插件並配置Maven的相關參數。

Maven的倉庫默認配置是連接Maven提供的一個倉庫,它提供了目前主流項目所需的所有依賴信息。Maven的這部分配置也在本文中略去,不懂得同學可以去網上查詢。

(4) 導入Maven頂級工程到Eclipse中。

在Eclipse的Package Explorer中單擊右鍵 -> Import… -> Maven –> 選中Existing Maven Projects -> 單擊Next –> 在彈出的select root folder窗口中單擊Browser…按鈕 -> 在彈出的窗口中找到剛纔創建的目錄並選中pom.xml文件 –> 單擊OK -> Finish即可將這個工程導入到Eclipse中。Maven的頂級工程已經創建完畢。

(5) 添加S2SH依賴。

在頂級工程的pom.xml文件中添加S2SH的依賴項,如下所示:

複製代碼
<properties>

<org.springframework.version>3.0.5.RELEASE</org.springframework.version>

</properties>

<dependencies>

<!-- add spring libraries -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Expression Language (depends on spring-core) Define this if you use

Spring Expression APIs (org.springframework.expression.*) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-expression</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define

this if you use Spring Bean APIs (org.springframework.beans.*) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core,

spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Application Context (depends on spring-core, spring-expression, spring-aop,

spring-beans) This is the central artifact for Spring's Dependency Injection

Container and is generally always defined -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Various Application Context utilities, including EhCache, JavaMail,

Quartz, and Freemarker integration Define this if you need any of these integrations -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Transaction Management Abstraction (depends on spring-core, spring-beans,

spring-aop, spring-context) Define this if you use Spring Transactions or

DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context,

spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA,

and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx)

Define this if you need ORM (org.springframework.orm.*) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-orm</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB,

JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans,

spring-context) Define this if you need OXM (org.springframework.oxm.*) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-oxm</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Web application development utilities applicable to both Servlet and

Portlet Environments (depends on spring-core, spring-beans, spring-context)

Define this if you use Spring MVC, or wish to use Struts, JSF, or another

web framework with Spring (org.springframework.web.*) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans,

spring-context, spring-web) Define this if you use Spring MVC with a Servlet

Container such as Apache Tomcat (org.springframework.web.servlet.*) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans,

spring-context, spring-web) Define this if you use Spring MVC with a Portlet

Container (org.springframework.web.portlet.*) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc-portlet</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!-- Support for testing Spring applications with tools such as JUnit and

TestNG This artifact is generally always defined with a 'test' scope for

the integration testing framework and unit testing stubs -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${org.springframework.version}</version>

<scope>test</scope>

</dependency>

<!-- end spring dependence -->

<!-- add hibernate library -->

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-entitymanager</artifactId>

<version>4.1.0.Final</version>

</dependency>

<!-- end add hibernate library -->

<!-- Gson: Java to Json conversion -->

<dependency>

<groupId>com.google.code.gson</groupId>

<artifactId>gson</artifactId>

<version>2.1</version>

<scope>compile</scope>

</dependency>

<!-- end add Gson -->

<dependency>

<groupId>javassist</groupId>

<artifactId>javassist</artifactId>

<version>3.8.0.GA</version>

</dependency>

<!-- add struts2 libiary -->

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-core</artifactId>

<version>2.3.16</version>

</dependency>

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-spring-plugin</artifactId>

<version>2.3.16</version>

</dependency>

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-json-plugin</artifactId>

<version>2.3.16</version>

</dependency>

<!-- end add struts library -->

<!-- xwork -->

<dependency>

<groupId>org.apache.struts.xwork</groupId>

<artifactId>xwork-core</artifactId>

<version>2.3.16</version>

</dependency>

<!-- xwork -->

<!-- commons-io -->

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>2.4</version>

</dependency>

<!-- commons-io -->

<dependency>

<groupId>com.sun</groupId>

<artifactId>tools</artifactId>

<version>1.6.0</version>

<scope>system</scope>

<systemPath>${env.JAVA_HOME}\lib\tools.jar</systemPath>

</dependency>

<!-- common -->

<dependency>

<groupId>org.apache.tomcat</groupId>

<artifactId>servlet-api</artifactId>

<version>${servlet-api_rel}</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>${junit_rel}</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>${log4j_rel}</version>

</dependency>

<dependency>

<groupId>net.sf.json-lib</groupId>

<artifactId>json-lib</artifactId>

<version>2.3</version>

<classifier>jdk15</classifier>

</dependency>

</dependencies>
複製代碼

 

以上是S2SH框架所需的所有的依賴關係及引入了處理JSON格式信息的依賴關係。所有這些依賴關係都能從Maven的主倉庫中獲取到。如果不清楚這方面的原理,請惡補一下Maven的相關知識。

(6) 新建一個Maven的Web Module工程。

創建這個工程有多種方法,可以使用Maven提供的模板創建,但我可能是由於網絡的問題,從來都沒有成功過。最後採用創建普通的Web工程,然後通過改造的方式來轉換成Web Module工程的方式。關於這個轉換過程,將在後續的文章中給出介紹,請關注。創建完成的工程如下所示:

clip_image001

如果不清楚怎麼建立這個Maven Web Module工程,可以參考我的另外一篇文章:使用Java web工程建立Maven Web Module工程

(7) web.xml中添加S2SH的配置信息。

添加Struts2的過濾器配置:

複製代碼
<!-- add struts2 configiguration -->

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- end add struts2 configuration

add spring configuration -->
複製代碼

 

添加Spring監聽器配置:

複製代碼
<listener>

<listener-class>com.dashboard.initsystem.SpringContextLoaderListener</listener-class>

</listener>

<param-name>contextConfigLocation</param-name>

<param-value>

classpath*:applicationContext.xml,

classpath*:/CONFIGS-INF/applicationContext-login.xml, classpath*:/CONFIGS-INF/applicationContext-dashboard.xml

</param-value>
複製代碼

 

紅色部分的目的是將Spring的配置文件分解成多個小的配置文件,以達到模塊化開發的目的。

(8) 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>

<constant name="struts.i18n.encoding" value="UTF-8"></constant>

<!-- caution: in release version, this value must be set false -->

<constant name="struts.devMode" value="true"></constant>

<include file="CONFIGS-INF/struts-login.xml"></include>

<include file="CONFIGS-INF/struts-dashboard.xml"></include>

</struts>
複製代碼

 

紅色部分表示其包含的其他子struts配置文件,這裏這樣劃分的目的也是爲了更好的支持模塊化開發。

(9) applicationContext.xml文件配置

複製代碼
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!--數據庫連接配置選項 -->

<bean id="DataSource"

class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName"

value="java:jboss/datasources/MYSQL">

</property>

</bean>

<!-- end 數據庫連接配置選項 -->

<beans>
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章