随笔 - 0, 文章 - 11, 评论 - 0, 引用 - 0
数据加载中……

徒手搭建 Struts2/Spring2/Hibernate3 集成开发环境

此环境为:Struts-2.2.3 + Spring-2.5.6 + Hibernate-3.6.8

所需要jar包如下:


struts2:
struts2-core-2.2.3.jar
struts2-spring-plugin-2.2.3.jar
xwork-core-2.2.3.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
commons-fileupload-1.2.2.jar
freemarker-2.3.16.jar
ognl-3.0.1.jar
javassist-3.12.0.GA.jar(hibernate同样需要)

spring:
spring.jar
commons-logging-1.1.1.jar
common-annotations.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
(如果用BasicDataSource来配置数据库连接,还要加入以下2个包)
commons-dbcp.jar
commons-pool.jar

hibernate:
hibernate3.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
slf4j-nop-1.6.4.jar(这个jar包要去slf4j官网下载slf4j-1.6.4集成包)

jdbc:
mysql-jdbc-driver.jar(MySQL5.1)

工程目录结构如下:

Web.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 6     <welcome-file-list>
 7         <welcome-file>index.jsp</welcome-file>
 8     </welcome-file-list>
 9 
10     <!-- 配置CharacterEncoding,设置字符集 -->
11     <filter>
12         <filter-name>characterEncodingFilter</filter-name>
13         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
14             <init-param>
15                 <param-name>encoding</param-name>
16                 <param-value>UTF-8</param-value>
17             </init-param>
18             <init-param>
19                 <param-name>forceEncoding</param-name>
20                 <param-value>true</param-value>
21             </init-param>
22     </filter>
23     <filter-mapping>
24         <filter-name>characterEncodingFilter</filter-name>
25         <url-pattern>/*</url-pattern>
26     </filter-mapping>
27 
28     <!-- 将HibernateSession开关控制配置在Filter,保证一个请求一个session,并对lazy提供支持 -->
29     <filter>
30         <filter-name>hibernateFilter</filter-name>
31         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
32         <init-param>
33             <param-name>singleSession</param-name>
34             <param-value>true</param-value>
35         </init-param>
36     </filter>
37     <filter-mapping>
38         <filter-name>hibernateFilter</filter-name>
39         <url-pattern>*.do</url-pattern>
40     </filter-mapping>
41 
42     <!-- 配置 Start的启动类 -->
43     <filter>
44         <filter-name>struts2</filter-name>
45         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
46     </filter>
47     <filter-mapping>
48         <filter-name>struts2</filter-name>
49         <url-pattern>/*</url-pattern>
50     </filter-mapping>
51     
52     <!-- 配置spring --> 
53    <context-param>
54         <param-name>contextConfigLocation</param-name>
55         <param-value>classpath:applicationContext-*.xml</param-value>
56     </context-param>
57     <listener>
58         <listener-class>
59             org.springframework.web.context.ContextLoaderListener
60         </listener-class>
61     </listener>
62 
63    <!-- 页面session配置 --> 
64    <session-config>  
65      <session-timeout>20</session-timeout>  
66    </session-config>  
67 
68    <!-- 错误页面 --> 
69    <error-page>  
70      <error-code>404</error-code>  
71      <location>/error/error404.html</location>  
72    </error-page>  
73    
74 </web-app>

struts.xml
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7 
 8     <!-- 将Action的创建交给spring来管理 --> 
 9     <constant name="struts.objectFactory" value="spring" />  
10     
11     <!-- 更改struts2请求Action的后缀名,默认为action。若想去掉后缀,设为","即可 --> 
12     <!-- <constant name="struts.action.extension" value="do" /> -->
13     
14     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
15     
16     <constant name="struts.devMode" value="true" />
17 
18     <package name="default" namespace="/" extends="struts-default">
19 
20         <default-action-ref name="index" />
21 
22         <global-results>
23             <result name="error">/error/error.jsp</result>
24         </global-results>
25 
26         <global-exception-mappings>
27             <exception-mapping exception="java.lang.Exception" result="error"/>
28         </global-exception-mappings>
29 
30         <action name="login" class="searchAction">
31             <result>/success.jsp</result>
32             <result name="error" type="redirect">/error/error.jsp</result>      
33         </action>
34     </package>
35 
36 
37     <!-- Add packages here -->
38 
39 </struts>

applicationContext-common.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:aop="http://www.springframework.org/schema/aop" 
 5         xmlns:context="http://www.springframework.org/schema/context" 
 6         xmlns:tx="http://www.springframework.org/schema/tx" 
 7         xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8                             http://www.springframework.org/schema/beans/spring-beans.xsd  
 9                             http://www.springframework.org/schema/aop 
10                             http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
11                             http://www.springframework.org/schema/context 
12                             http://www.springframework.org/schema/context/spring-context-2.5.xsd  
13                             http://www.springframework.org/schema/tx 
14                             http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
15 
16     <!-- 代替 Hibernate 配置访问数据库连接 -->
17     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
18         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
19         <property name="url" value="jdbc:mysql://localhost:3306/hibernate3x"></property>
20         <property name="username" value="root"></property>
21         <property name="password" value="123456"></property>
22     </bean>
23     
24     <!--  配置SessionFactory -->
25     <bean id="sessionFactory"
26         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
27         <property name="dataSource">
28             <ref bean="dataSource" />
29         </property>
30         <property name="hibernateProperties">
31             <props>
32                 <prop key="hibernate.dialect">
33                     org.hibernate.dialect.MySQLDialect
34                 </prop>
35             </props>
36         </property>
37         <property name="mappingResources">
38             <list>
39                 <value>com/lmm/ssh/domain/Classes.hbm.xml</value>
40                 <value>com/lmm/ssh/domain/Student.hbm.xml</value>
41                 <value>com/lmm/ssh/domain/Teacher.hbm.xml</value></list>
42         </property>
43     </bean>
44     
45     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
46         <property name="sessionFactory">
47             <ref bean="sessionFactory" />
48         </property>
49     </bean>
50         
51     <tx:advice id="txAdvice" transaction-manager="transactionManager">
52         <tx:attributes>
53             <tx:method name="append*" propagation="REQUIRED"/>
54             <tx:method name="remove*" propagation="REQUIRED"/>
55             <tx:method name="modify*" propagation="REQUIRED"/>
56             <tx:method name="*" read-only="true"/>
57         </tx:attributes>
58     </tx:advice>
59     
60     <aop:config>
61         <aop:pointcut id="all" expression="execution(* com.lmm.ssh.service.impl.*.*(..))"/>
62         <aop:advisor advice-ref="txAdvice" pointcut-ref="all"/>
63     </aop:config>
64 </beans>

applicationContext-dao.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 6 
 7 
 8     <bean id="userDaoRef" class="com.lmm.ssh.dao.impl.UserDaoHibernateImpl">
 9         <property name="sessionFactory" ref="sessionFactory"></property>
10     </bean>
11 </beans>

applicationContext-service.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 6 
 7 
 8     <bean id="userServiceRef" class="com.lmm.ssh.service.impl.UserServiceImpl">
 9         <property name="userDaoImpl" ref="userDaoRef"></property>
10     </bean>
11     
12 </beans>

applicationContext-action.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 6 
 7     <bean name="searchAction" class="com.lmm.ssh.web.action.SearchAction">
 8         <property name="userServiceImpl" ref="userServiceRef"></property>
 9     </bean>
10     
11 </beans>


 1 package com.lmm.ssh.web.action;
 2 
 3 import com.lmm.ssh.service.IUserService;
 4 
 5 public class SearchAction extends BaseAction {
 6     private IUserService userServiceImpl ;
 7 
 8     public IUserService getUserServiceImpl() {
 9         return userServiceImpl;
10     }
11 
12     public void setUserServiceImpl(IUserService userServiceImpl) {
13         this.userServiceImpl = userServiceImpl;
14     }
15     
16     public String search () {
17         return SUCCESS;
18     }
19 }

 1 package com.lmm.ssh.service.impl;
 2 
 3 import com.lmm.ssh.dao.IUserDao;
 4 import com.lmm.ssh.service.IUserService;
 5 
 6 public class UserServiceImpl implements IUserService {
 7     private IUserDao userDaoImpl ;
 8 
 9     public IUserDao getUserDaoImpl() {
10         return userDaoImpl; 
11     }
12 
13     public void setUserDaoImpl(IUserDao userDaoImpl) {
14         this.userDaoImpl = userDaoImpl;
15     }
16     
17 }
18 

1 package com.lmm.ssh.dao.impl;
2 
3 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
4 
5 import com.lmm.ssh.dao.IBaseDao;
6 
7 public class BaseDaoHibernateImpl extends HibernateDaoSupport implements IBaseDao {
8 
9 }

1 package com.lmm.ssh.dao.impl;
2 
3 import org.hibernate.Query;
4 import com.lmm.ssh.dao.IUserDao;
5 
6 public class UserDaoHibernateImpl extends BaseDaoHibernateImpl implements IUserDao {
7 
8 }

至此,Struts-2.2.3 + Spring-2.5.6 + Hibernate-3.6.8 整合完成。

posted on 2014-05-10 20:18 00001000 阅读(115) 评论(0)  编辑  收藏 所属分类: Struts2/Spring2/Hibernate3


只有注册用户登录后才能发表评论。


网站导航: