Maven+struts2+spring4+hibernate4的环境搭建

时间:2022-10-24 05:03:08

    搭建Maven+struts2+spring4+hibernate4其实并不难!但开始弄的时候还是费了我好大的力气,老是出现这样那样的错误!好了,废话不多说,开始搭建开发环境。

一、Myeclipse下Maven配置

   建议采用Myeclipse,我用的是Myeclipse 2014 spring版。Maven的安装我就不多说了,网上有很多资料可查!

1、在Myeclipse下配置Maven,如图

Maven+struts2+spring4+hibernate4的环境搭建

Maven+struts2+spring4+hibernate4的环境搭建

2、新建web project

Maven+struts2+spring4+hibernate4的环境搭建

一直next

Maven+struts2+spring4+hibernate4的环境搭建

然后finish!

Maven+struts2+spring4+hibernate4的环境搭建

3、配置pom.xml

  1 <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">
  2   <modelVersion>4.0.0</modelVersion>
  3   <groupId>org.maven</groupId>
  4   <artifactId>mssh-2</artifactId>
  5   <version>0.0.1-SNAPSHOT</version>
  6   <packaging>war</packaging>
  7   <name>mssh-1</name>
  8   <description/>
  9   <properties>
 10     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 11       <spring.version>4.1.6.RELEASE</spring.version>
 12   </properties>
 13   <build>
 14     <plugins>
 15       <plugin>
 16         <artifactId>maven-compiler-plugin</artifactId>
 17         <version>2.3.2</version>
 18         <configuration>
 19           <source>1.6</source>
 20           <target>1.6</target>
 21         </configuration>
 22       </plugin>
 23       <plugin>
 24         <artifactId>maven-war-plugin</artifactId>
 25         <version>2.2</version>
 26         <configuration>
 27           <version>3.0</version>
 28           <failOnMissingWebXml>false</failOnMissingWebXml>
 29         </configuration>
 30       </plugin>
 31     <plugin>
 32         <groupId>org.apache.tomcat.maven</groupId>
 33         <artifactId>tomcat7-maven-plugin</artifactId>
 34         <version>2.1</version>
 35     </plugin>
 36     </plugins>
 37   </build>
 38   <dependencies>
 39    <!-- jstl -->
 40         <dependency>
 41             <groupId>javax.servlet</groupId>
 42             <artifactId>jstl</artifactId>
 43             <version>1.2</version>
 44         </dependency>
 45 
 46         <!--jsp-->
 47         <dependency>
 48             <groupId>javax.servlet.jsp</groupId>
 49             <artifactId>jsp-api</artifactId>
 50             <version>2.2.1-b03</version>
 51             <scope>provided</scope>
 52         </dependency>
 53 
 54          <!-- servlet3 -->
 55         <dependency>
 56             <groupId>javax.servlet</groupId>
 57             <artifactId>javax.servlet-api</artifactId>
 58             <version>3.0.1</version>
 59             <scope>provided</scope>
 60         </dependency>
 61 
 62         <!-- fastJson -->
 63         <dependency>
 64             <groupId>com.alibaba</groupId>
 65             <artifactId>fastjson</artifactId>
 66             <version>1.1.24</version>
 67         </dependency>
 68 
 69         <!--junit-->
 70         <dependency>
 71             <groupId>junit</groupId>
 72             <artifactId>junit</artifactId>
 73             <version>4.11</version>
 74             <scope>test</scope>
 75         </dependency>
 76        
 77         <!-- log4j -->
 78         <dependency>
 79             <groupId>log4j</groupId>
 80             <artifactId>log4j</artifactId>
 81             <version>1.2.17</version>
 82         </dependency>
 83   
 84       <!-- Spring -->
 85         <dependency>
 86             <groupId>org.springframework</groupId>
 87             <artifactId>spring-core</artifactId>
 88             <version>${spring.version}</version>
 89         </dependency>
 90         <dependency>
 91             <groupId>org.springframework</groupId>
 92             <artifactId>spring-expression</artifactId>
 93             <version>${spring.version}</version>
 94         </dependency>
 95 
 96       
 97         <dependency>
 98             <groupId>org.springframework</groupId>
 99             <artifactId>spring-beans</artifactId>
100             <version>${spring.version}</version>
101         </dependency>
102 
103         
104         <dependency>
105             <groupId>org.springframework</groupId>
106             <artifactId>spring-aop</artifactId>
107             <version>${spring.version}</version>
108         </dependency>
109         
110         <dependency>
111             <groupId>org.springframework</groupId>
112             <artifactId>spring-context</artifactId>
113             <version>${spring.version}</version>
114         </dependency>
115 
116         <dependency>
117             <groupId>org.springframework</groupId>
118             <artifactId>spring-context-support</artifactId>
119             <version>${spring.version}</version>
120         </dependency>
121 
122         <dependency>
123             <groupId>org.springframework</groupId>
124             <artifactId>spring-tx</artifactId>
125             <version>${spring.version}</version>
126         </dependency>
127 
128         
129         <dependency>
130             <groupId>org.springframework</groupId>
131             <artifactId>spring-orm</artifactId>
132             <version>${spring.version}</version>
133         </dependency>
134 
135         
136 
137        <!--springmvc-->
138         <dependency>
139             <groupId>org.springframework</groupId>
140             <artifactId>spring-web</artifactId>
141             <version>${spring.version}</version>
142         </dependency>
143 
144         <dependency>
145             <groupId>org.springframework</groupId>
146             <artifactId>spring-webmvc</artifactId>
147             <version>${spring.version}</version>
148         </dependency>
149         
150         <dependency>
151             <groupId>org.springframework</groupId>
152             <artifactId>spring-webmvc-portlet</artifactId>
153             <version>${spring.version}</version>
154         </dependency>
155 
156         <dependency>
157             <groupId>org.springframework</groupId>
158             <artifactId>spring-test</artifactId>
159             <version>${spring.version}</version>
160             <scope>test</scope>
161         </dependency>
162 
163       <!-- aspectjweaver-->
164         <dependency>
165             <groupId>org.aspectj</groupId>
166             <artifactId>aspectjweaver</artifactId>
167             <version>1.8.4</version>
168         </dependency>
169 
170         <!-- hibernate4 -->
171         <dependency>
172             <groupId>org.hibernate</groupId>
173             <artifactId>hibernate-core</artifactId>
174             <version>4.3.11.Final</version>
175         </dependency>
176         
177          <!-- struts2 -->
178         <dependency>
179             <groupId>org.apache.struts</groupId>
180             <artifactId>struts2-core</artifactId>
181             <version>2.3.4.1</version>
182             <exclusions>
183                 <exclusion><!--javassist与hibernate中的javassist冲突-->
184                     <groupId>javassist</groupId>
185                     <artifactId>javassist</artifactId>
186                 </exclusion>
187             </exclusions>
188         </dependency>
189         <!--spring整合struts2插件-->
190         <dependency>
191             <groupId>org.apache.struts</groupId>
192             <artifactId>struts2-spring-plugin</artifactId>
193             <version>2.3.4.1</version>
194         </dependency>
195         <!--struts2注解插件-->
196         <dependency>
197             <groupId>org.apache.struts</groupId>
198             <artifactId>struts2-convention-plugin</artifactId>
199             <version>2.3.4.1</version>
200         </dependency>
201         
202          <!--数据库-->
203         <!-- mysql的依赖 -->
204         <dependency>
205             <groupId>mysql</groupId>
206             <artifactId>mysql-connector-java</artifactId>
207             <version>5.1.21</version>
208         </dependency>
209 
210         <!-- c3p0 -->
211         <dependency>
212             <groupId>c3p0</groupId>
213             <artifactId>c3p0</artifactId>
214             <version>0.9.1.2</version>
215         </dependency>
216 
217        <!-- druid -->
218         <dependency>
219             <groupId>com.alibaba</groupId>
220             <artifactId>druid</artifactId>
221             <version>0.2.8</version>
222         </dependency>
223   </dependencies>
224 </project>

 

4、其他配置

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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">
 3 <!-- spring监听器 -->
 4   <context-param>
 5     <param-name>contextConfigLocation</param-name>
 6     <param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
 7   </context-param>
 8   <listener>
 9     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
10   </listener>
11   
12   <!-- 数据提交中文乱码问题 -->
13     <filter>
14         <filter-name>encodingFilter</filter-name>
15         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
16         <init-param>
17             <param-name>encoding</param-name>
18             <param-value>UTF-8</param-value>
19         </init-param>
20     </filter>
21     <filter-mapping>
22         <filter-name>encodingFilter</filter-name>
23         <url-pattern>/*</url-pattern>
24     </filter-mapping>
25   
26   <!-- openSessionInView配置 -->
27     <filter>
28         <filter-name>openSessionInViewFilter</filter-name>
29         <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
30         <init-param>
31             <param-name>singleSession</param-name>
32             <param-value>true</param-value>
33         </init-param>
34     </filter>
35     
36   <!-- struts2 -->
37   <filter>
38     <filter-name>struts2</filter-name>
39     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
40   </filter>
41   <filter-mapping>
42     <filter-name>struts2</filter-name>
43     <url-pattern>/*</url-pattern>
44   </filter-mapping>
45   
46   <welcome-file-list>
47     <welcome-file>index.jsp</welcome-file>
48   </welcome-file-list>
49 </web-app>

spring配置、spring整合Hibernate配置、stuts配置、日志打印配置、数据库配置

Maven+struts2+spring4+hibernate4的环境搭建

spring.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 8 
 9 <!-- 引入属性 -->
10 <context:property-placeholder location="classpath:db.properties"/>
11 <!-- 自动扫描dao、service -->
12 <context:component-scan base-package="sy.dao,sy.service"/>
13 </beans>
<!--spring-hibernate.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- JNDI方式配置数据源 --> <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> --> <!-- 配置druid数据源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.jdbcUrl}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="0" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="20" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="20" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> <!-- 监控数据库 --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergeStat" /> </bean> <!-- 配置 C3P0 数据源 --> <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> --> <!-- 配置 SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 配置Hibernate相关属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property> <!-- 自动扫描注解方式配置实体bean --> <property name="packagesToScan" value="sy.model"/> </bean> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" /> <tx:method name="save*" /> <tx:method name="update*" /> <tx:method name="modify*" /> <tx:method name="edit*" /> <tx:method name="delete*" /> <tx:method name="remove*" /> <tx:method name="repair" /> <tx:method name="deleteAndRepair" />

<tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* sy.service..*Impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> </beans>

 

db.properties

 1 hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
 2 jdbc.user=root
 3 jdbc.password=123456
 4 jdbc.driverClass=com.mysql.jdbc.Driver
 5 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssh
 6 jdbc.initPoolSize=5
 7 jdbc.maxPoolSize=10
 8 
 9 
10 #hibernate.dialect=org.hibernate.dialect.OracleDialect
11 #jdbc.driverClass=oracle.jdbc.driver.OracleDriver
12 #jdbc.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:orcl
13 #jdbc.user=sshe
14 #jdbc.password=sshe
15 
16 #hibernate.dialect=org.hibernate.dialect.SQLServerDialect
17 #jdbc.driverClass=net.sourceforge.jtds.jdbc.Driver
18 #jdbc.url=jdbc:jtds:sqlserver://127.0.0.1:1433/sy
19 #jdbc.username=sa
20 #jdbc.password=123456
21 #jndiName=java:comp/env/dataSourceName
22 
23 hibernate.show_sql=true
24 hibernate.format_sql=true
25 hibernate.hbm2ddl.auto=update
26 
27 #uploadFieldName=filedata
28 #uploadFileMaxSize=20971520
29 #uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid
30 #uploadDirectory=attached
31 
32 #http://localhost:8080/mssh-1/userAction!list
33 #...

 

struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
 3 <struts>
 4 
 5     <!-- 指定由spring负责action对象的创建 -->
 6     <constant name="struts.objectFactory" value="spring" />
 7     <!-- 所有匹配*.action的请求都由struts2处理 -->
 8     <!--  
 9     <constant name="struts.action.extension" value="action,do" />
10     -->
11     <!-- 是否启用开发模式 -->
12     <constant name="struts.devMode" value="true" />
13     <!-- struts配置文件改动后,是否重新加载 -->
14     <constant name="struts.configuration.xml.reload" value="true" />
15     <!-- 设置浏览器是否缓存静态内容 -->
16     <constant name="struts.serve.static.browserCache" value="false" />
17     <!-- 请求参数的编码方式 -->
18     <constant name="struts.i18n.encoding" value="utf-8" />
19     <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
20     <constant name="struts.i18n.reload" value="true" />
21     <!-- 文件上传最大值 -->
22     <constant name="struts.multipart.maxSize" value="104857600" />
23     <!-- 让struts2支持动态方法调用 -->
24     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
25     <!-- Action名称中是否还是用斜线 -->
26     <constant name="struts.enable.SlashesInActionNames" value="false" />
27     <!-- 允许标签中使用表达式语法 -->
28     <constant name="struts.tag.altSyntax" value="true" />
29     <!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
30     <constant name="struts.dispatcher.parametersWorkaround" value="false" />
31 
32     <package name="basePackage" extends="struts-default">
33     </package>
34 
35 </struts>

 

log4j.xml

 1 log4j.rootLogger=INFO,A1,R
 2 
 3 log4j.appender.A1=org.apache.log4j.ConsoleAppender
 4 log4j.appender.A1.Target=System.out
 5 log4j.appender.A1.layout=org.apache.log4j.PatternLayout
 6 log4j.appender.A1.layout.ConversionPattern=[%c]%m%n
 7 
 8 log4j.appender.R=org.apache.log4j.RollingFileAppender 
 9 log4j.appender.R.File=sshe.log
10 log4j.appender.R.MaxFileSize=10MB
11 log4j.appender.R.Threshold=ALL
12 log4j.appender.R.layout=org.apache.log4j.PatternLayout
13 log4j.appender.R.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n