Struts2+spring+jdbc 以xml配置形式整合

来源:互联网 发布:索尼电视是什么软件 编辑:程序博客网 时间:2024/05/21 11:30

今天做作业,练习一下Struts2+spring+jdbc 以xml配置形式整合

整合步骤:


工程结构图:


重要配置文件

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">

  <display-name></display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  

   <listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring/applicationContext.xml</param-value>

</context-param>

  

  <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>

</web-app>

 

其中spring通过监听器整合,struts2通过过滤器整合。

 

 

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/aop 

           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-2.5.xsd

           http://www.springframework.org/schema/tx 

           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<import resource="applicationContext-db.xml" />

<import resource="applicationContext-ordertable.xml" />

<import resource="applicationContext-customer.xml" />

</beans>

applicationContext-db.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/aop 

           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-2.5.xsd

           http://www.springframework.org/schema/tx 

           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!--     用dbcp配置

 产生dataSource

     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>classpath:jdbc/jdbc_dbcp.properties</value>

</property>

 </bean>

     <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>

-->

  <!-- 产生dataSource  用c3p0配置-->

     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>classpath:jdbc/jdbc_c3p0.properties</value>

</property>

 </bean>

     <bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${jdbc_c3p0.driverClass}" />

<property name="jdbcUrl" value="${jdbc_c3p0.driverClass.jdbcUrl}" />

<property name="user" value="${jdbc_c3p0.user}" />

<property name="password" value="${jdbc_c3p0.password}" />

</bean>

<!-- 

声明事务通知 

id事务标识 transaction-manager 

-->

<!-- 事务管理器 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource">

<ref bean="dataSource"/>

</property>

</bean>

<!-- 声明目标方法中哪些方法需要事务,哪些不需要事务 -->

<tx:advice id="tx" transaction-manager="transactionManager">

<tx:attributes>

<!-- 

name 限定方法的名称            

isolation 隔离机制 

propagation传播机制

ready-only 只读

 -->

<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />

</tx:attributes>

</tx:advice>

            <!-- spring框架做的事情 -->

  <aop:config>

<aop:pointcut expression="execution(* lr.service.impl.CustomerServiceImpl.*(..))" id="perform" />

<aop:advisor advice-ref="tx" pointcut-ref="perform"/>

</aop:config>

</beans>

applicationContext-ordertable.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/aop 

           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-2.5.xsd

           http://www.springframework.org/schema/tx 

           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

           

<bean id="ordertableDao" class="lr.dao.impl.OrdertableDaoImpl">

<property name="dataSource">

<ref bean="dataSource"/>

</property>

</bean>

<bean id="ordertableService" class="lr.service.impl.OrdertableServiceImpl">

<property name="ordertableDao">

<ref bean="ordertableDao"/>

</property>

<property name="customerDao">

<ref bean="customerDao"/>

</property>

</bean>

<bean id="ordertableAction" class="lr.action.OrdertableAction">

<property name="ordertableService">

<ref bean="ordertableService"/>

</property>

</bean>

</beans>

 

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"

"http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>

   <!-- 配置文件改了以后不用重新启动 -->

   <constant name="struts.devMode" value="true"/>

  

   <!-- 把struts的请求委托给spring管理,

        作用:创建Action实例的过程由spring处理,其他的还是有struts2自己处理 -->

   <constant name="struts.objectFactory" value="spring" />

   <include file="struts2/struts-ordertable.xml"></include>

   <include file="struts2/struts-customer.xml"></include>

</struts>

struts-customer.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"

"http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>

   <package name="customer" extends="struts-default" namespace="/">

    <action name="customerAction_*" method="{1}" class="customerAction">

    <result name="success">/savecustomer.jsp</result>

    </action>

   </package>

</struts>


所需jar包下载 

http://download.csdn.net/detail/u012814506/6709269

工程下载

http://download.csdn.net/detail/u012814506/6709313


 




0 0