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

来源:互联网 发布:云墙vpn端口不可用 编辑:程序博客网 时间:2024/06/05 20:03

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 useSpring 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) Definethis 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 InjectionContainer 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 orDAO 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 andPortlet Environments (depends on spring-core, spring-beans, spring-context)Define this if you use Spring MVC, or wish to use Struts, JSF, or anotherweb 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 ServletContainer 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 PortletContainer (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 andTestNG This artifact is generally always defined with a 'test' scope forthe 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 configurationadd 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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://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>
复制代码
0 0
原创粉丝点击