Struts2+Spring3.0+Hibernate3.2框架搭建全程

 

Struts2+Spring3.0+Hibernate3.2配置:

一、引入必须的jar:

1、Struts2必须的jar:

2、Spring3.0所必须的jar

(根据需要还需要加入一些jar,下面是Spring的所有jar,也可以将其都引入项目中:)

3、Hibernate3.2必须jar

4、其他一些需要的jar包:

二、配置文件:

1、struts:(struts.xml,放到src目录下,且最好文件名叫struts.xml)

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>     
  7.       <package name="test" extends="struts-default" namespace="">  
  8.           <action name="test" class="testAction">  
  9.                 <!--   
  10.                     name属性是页面提交时候访问的action也就是form的action。  
  11.                     class属性是和spring相结合的地方,这里配的是spring bean的id -->  
  12.               <result name="success">/result.jsp</result>  
  13.                 <!-- result 是返回值 -->  
  14.           </action>  
  15.       </package>  
  16. </struts>  

2、hibernate:(hibernate.cfg.xml,放到src/config目录下,具体的每个参数就不解释了)

[html] view plain copy
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  3.   
  4. <hibernate-configuration>  
  5.   <session-factory>  
  6.     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  7.     <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>  
  8.     <property name="hibernate.connection.username">root</property>  
  9.     <property name="hibernate.connection.password">admin</property>  
  10.     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  11.     <property name="hibernate.show_sql">true</property>  
  12.     <property name="hibernate.hbm2ddl.auto">update</property>  
  13.     <property name="hibernate.current_session_context_class">thread</property>  
  14.       
  15.       
  16.     <mapping resource="com/dcy/test/model/Organization.hbm.xml"/>  
  17.     <mapping resource="com/dcy/test/model/Person.hbm.xml"/>  
  18.   </session-factory>  
  19. </hibernate-configuration>  

3、spring:(applicationContext-*.xml,src/config目录下)

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xmlns:tx="http://www.springframework.org/schema/tx"  
  7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  10.     <!-- 配置SessionFactory -->  
  11.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  12.         <property name="configLocation">  
  13.             <value>classpath:config/hibernate.cfg.xml</value>  
  14.         </property>  
  15.     </bean>  
  16.       
  17.     <!-- 配置事务管理器 -->  
  18.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  19.         <property name="sessionFactory">  
  20.             <ref bean="sessionFactory"/>            
  21.         </property>  
  22.     </bean>  
  23.       
  24.     <!-- 那些类那些方法使用事务 -->  
  25.     <aop:config>  
  26.         <aop:pointcut id="allManagerMethod" expression="execution(* com.dcy.oa.dao.impl.*.*(..))"/>  
  27.         <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>  
  28.     </aop:config>  
  29.       
  30.     <!-- 事务的传播特性 -->    
  31.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  32.         <tx:attributes>  
  33.             <tx:method name="add*" propagation="REQUIRED"/>  
  34.             <tx:method name="del*" propagation="REQUIRED"/>  
  35.             <tx:method name="modify*" propagation="REQUIRED"/>  
  36.             <tx:method name="*" propagation="REQUIRED" read-only="true"/>  
  37.         </tx:attributes>  
  38.     </tx:advice>  
  39. </beans>  

(下面是测试用的bean)

[html] view plain copy
  1. <bean id="testAction" class="com.dcy.oa.web.action.TestAction">  
  2.     <property name="organizationDao" ref="organizationDao"/>  
  3. </bean>  

4、web.xml

[html] view plain copy
  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.   
  7.     <!-- 配置struts2 -->  
  8.     <filter>  
  9.         <filter-name>struts2</filter-name>  
  10.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  11.               
  12.     </filter>  
  13.     <filter-mapping>  
  14.         <filter-name>struts2</filter-name>  
  15.         <url-pattern>/*</url-pattern>  
  16.     </filter-mapping>  
  17.   
  18.     <!-- 配置spring -->  
  19.     <listener>  
  20.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  21.     </listener>  
  22.     <context-param>  
  23.         <param-name>contextConfigLocation</param-name>  
  24.         <param-value>  
  25.             classpath:config/applicationContext-common.xml,  
  26.             classpath:config/applicationContext*.xml  
  27.         </param-value>  
  28.     </context-param>  
  29.       
  30.       
  31.     <welcome-file-list>  
  32.         <welcome-file>index.jsp</welcome-file>  
  33.     </welcome-file-list>  
  34. </web-app>  

 三、测试action

[java] view plain copy
  1. import com.opensymphony.xwork2.ActionSupport;  
  2.   
  3. public class TestAction extends ActionSupport {  
  4.       
  5.     @Override  
  6.     public String execute() throws Exception {  
  7.           
  8.         return "success";  
  9.     }  
  10. }  
[java] view plain copy
  1. 四、测试用jsp页面:  
[java] view plain copy
  1. <pre name="code" class="html"><body>  
  2.     This is my JSP page. <br>  
  3.     <form action="test" method="post">      
  4.         <input type="submit" value="submit"/>  
  5.     </form>          
  6.   </body></pre><br>  
  7. <br>  
  8. <pre></pre>  
  9. <pre class="java" name="code"><p align="left">  
  10. </p></pre> 

你可能感兴趣的:(搭建,的)