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)
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="test" extends="struts-default" namespace="">
- <action name="test" class="testAction">
- <!--
- name属性是页面提交时候访问的action也就是form的action。
- class属性是和spring相结合的地方,这里配的是spring bean的id -->
- <result name="success">/result.jsp</result>
- <!-- result 是返回值 -->
- </action>
- </package>
- </struts>
2、hibernate:(hibernate.cfg.xml,放到src/config目录下,具体的每个参数就不解释了)
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">admin</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.show_sql">true</property>
- <property name="hibernate.hbm2ddl.auto">update</property>
- <property name="hibernate.current_session_context_class">thread</property>
- <mapping resource="com/dcy/test/model/Organization.hbm.xml"/>
- <mapping resource="com/dcy/test/model/Person.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
3、spring:(applicationContext-*.xml,src/config目录下)
- <?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.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <!-- 配置SessionFactory -->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:config/hibernate.cfg.xml</value>
- </property>
- </bean>
- <!-- 配置事务管理器 -->
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref bean="sessionFactory"/>
- </property>
- </bean>
- <!-- 那些类那些方法使用事务 -->
- <aop:config>
- <aop:pointcut id="allManagerMethod" expression="execution(* com.dcy.oa.dao.impl.*.*(..))"/>
- <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
- </aop:config>
- <!-- 事务的传播特性 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="add*" propagation="REQUIRED"/>
- <tx:method name="del*" propagation="REQUIRED"/>
- <tx:method name="modify*" propagation="REQUIRED"/>
- <tx:method name="*" propagation="REQUIRED" read-only="true"/>
- </tx:attributes>
- </tx:advice>
- </beans>
(下面是测试用的bean)
- <bean id="testAction" class="com.dcy.oa.web.action.TestAction">
- <property name="organizationDao" ref="organizationDao"/>
- </bean>
4、web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <!-- 配置struts2 -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 配置spring -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:config/applicationContext-common.xml,
- classpath:config/applicationContext*.xml
- </param-value>
- </context-param>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
三、测试action
- import com.opensymphony.xwork2.ActionSupport;
- public class TestAction extends ActionSupport {
- @Override
- public String execute() throws Exception {
- return "success";
- }
- }
- 四、测试用jsp页面:
- <pre name="code" class="html"><body>
- This is my JSP page. <br>
- <form action="test" method="post">
- <input type="submit" value="submit"/>
- </form>
- </body></pre><br>
- <br>
- <pre></pre>
- <pre class="java" name="code"><p align="left">
- </p></pre>