Maven构建Struts2+Hibernate+Spring web项目

软件:

maven:3.3.9,jdk:1.7 ,Struts2:2.3.24.1,Hibernate:4.3.6,Spring:4.2.5,mysql:5.1.34,Junit:4,Myeclipse:2014;

源码下载:https://github.com/fansy1990/ssh_v3 

1. 构建Maven工程

1.1 Maven 设置:

选择Maven的本地路径:
Window -》 Preference -> Maven4Myeclipse

修改setting.xml(视情况修改,先可以不修改,如果下载jar包下载有问题时,再改)
<mirror>

        <id>nexus-osc</id>

        <mirrorOf>*</mirrorOf>

        <name>Nexus osc</name>

        <url>http://maven.oschina.net/content/groups/public/</url>
    </mirror>
设置为开源中国的资源库,这样下载快

1.2 JDK设置: 

新添加jdk,同时添加 Default VM Arguments: -Dmaven.multiModuleProjectDirectory=$M2_HOME

1.3 新建Maven工程

1)File -> New -> Project -> Maven Project 
 
2)选择webapp  catalog

3)设置 group id 和artifact id:

建好的工程如下(上面起名改为v3,而非v2):


2. 添加Web支持

1) 右键工程:Myeclipse -> Project Facets[Capabilities] -> Install Dynamic Web Module ;


不用添加tomcat的lib包到工程classpath中;


添加后的工程如下:



3. 添加Struts2、Hibernate、Spring

3.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>ssh</groupId>
	<artifactId>v2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>v2</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.34</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>4.3.6.Final</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.24.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-spring-plugin</artifactId>
			<version>2.3.24.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-junit-plugin</artifactId>
			<version>2.3.24.1</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.0</version>

			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>javax.servlet</groupId>
					<artifactId>servlet-api</artifactId>
					
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>

	</dependencies>
</project>


3.2 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>v3</display-name>
  <display-name>Struts </display-name>
 <!-- 添加对spring的支持 -->  
  <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>  
    <!-- 当hibernate+spring配合使用的时候,如果设置了lazy=true,那么在读取数据的时候,当读取了父数据后,  
     hibernate会自动关闭session,这样,当要使用子数据的时候,系统会抛出lazyinit的错误,  
      这时就需要使用spring提供的 OpenSessionInView,OpenSessionInView主要是保持Session状态  
      知道request将全部页面发送到客户端,这样就可以解决延迟加载带来的问题 --> 
    <filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
     <!-- 添加对struts2的支持 --> 
    <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>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>


3.3 添加resource目录(source),并修改输出目录:



拷贝下面的文件到resource目录:
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<constant name="struts.devMode" value="true" />
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<!--设置编码形式为UTF-8-->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 添加spring管理 -->
    <constant name="struts.objectFactory" value="spring"/>

<!-- 添加test action测试 -->
	<package name="test" extends="struts-default" namespace="/test">
		<action name="user" class="demo.action.UserAction">
			<result>/success.jsp</result>
		</action>
	</package> 

</struts>


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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">


	<!-- 自动加载Annotation bean -->  
	<context:annotation-config />
	<context:component-scan base-package="demo" />

	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="classpath:jdbc.properties" />
	</bean>
	<!--  配置dbcp数据源 -->
	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- Hibernate 4 SessionFactory Bean definition -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自动扫描注解方式配置的hibernate类文件 --> 
		<property name="packagesToScan">
			<list>
				<value>demo.model</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>
	
<!-- 	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean> -->
	
	<!-- <tx:annotation-driven transaction-manager="txManager" /> -->
	 <!-- 配置事务管理器 -->  
	 <bean id="txManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
  <!-- 配置事务切面 -->
	<aop:config >
		<aop:pointcut id="defaultServiceOperation" expression="execution(public * demo.service.*Service.*(..))" />

		<aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice" />
	</aop:config>

	<tx:advice id="defaultTxAdvice" transaction-manager="txManager">
		<tx:attributes>
		<!-- 先暂定所有方法都加事务 -->
			<tx:method name="*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice> 


</beans>



jdbc.properties:(根据实际情况修改)
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hibernate
jdbc.username=root
jdbc.password=root
log4j.properties:
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n


3.4 把maven jar包添加到lib中:

右键工程-> properties -> myeclipse -> Deployment Assembly 


做好上面的步骤后,其工程目录如下:

你可能感兴趣的:(Maven构建Struts2+Hibernate+Spring web项目)