Struts2+spring+jdbc 以xml配置形式整合
今天做作业,练习一下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
工程下载
Struts2+spring+jdbc 以xml配置形式整合相关推荐
- springboot项目中的注解 启动项目的方式 解决spring的bean.xml配置不生效 spring的基础JDBC配置
依赖 创建一个 Spring Boot 工程时,可以继承自一个 spring-boot-starter-parent ,也可以不继承 先来看 parent 的基本功能有哪些? 定义了 Java 编译版 ...
- Spring框架中XML配置特殊属性注入
Spring框架中XML配置特殊属性注入 前言 创建测试类 其他类型属性 前言 Spring框架中,在通过set方式进行属性注入时,可能会遇到某些特殊字符的注入,例如:null和某些标签字符" ...
- Spring JDBC-使用XML配置声明式事务
系列 概述 基于aop/tx命名空间的配置 示例 tx:method元素属性 系列 Spring对事务管理的支持概述以及 编程式的事务管理 Spring JDBC-使用XML配置声明式事务 Sprin ...
- spring事务管理-xml配置aop事务(重点)
刚才咱们是使用了模板操作咱们事务,当然使用模板操作比较low,还得写代码,每个方法都写太费劲了,首先把之前写的先注释掉,把这个transfer这个代码直接复制一份,然后底下展一份,留着注释掉就行了,上 ...
- Spring MVC 无XML配置入门示例
Spring MVC 无XML(纯 Java)配置入门示例 本示例是从<Spring in Action, Fourth Edition>一书而来,涉及的是书中5.1节部分内容,书中其实说 ...
- @bean注解和@component注解的区别_阿里面试题一:spring里面使用xml配置和注解配置区别...
阿里是众多程序员梦寐以求的地方,今天我们分享阿里的一个真实面试题,看似简单,回答起来确实没有头绪?下面我们分几点给大家说下答案. 适用场景 Xml配置场景: 1.Bean实现类来源于第三方类库,如Da ...
- Spring Boot 导入Xml配置
测试类: Service类: public class HelloService {public String gethello(){return "hello xml";} } ...
- spring-mybatis.xml 访问html5,Spring mvc无xml配置及利用JdbcTemplate访问数据库
项目结构 一.新建动态web项目取名HelloSpringMVC 二./WebContent/WEB-INF/lib下导入必要依赖库 commons-collections4-4.1.jar.comm ...
- IDEA右键快捷创建Xml文件模板 IDEA如何创建xml文件 Spring boot项目xml配置类模板
可扩展标记语言,标准通用标记语言的子集,简称XML.是一种用于标记电子文件使其具有结构性的标记语言. 在电子计算机中,标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种的信息比如 ...
最新文章
- linux shell let命令,shell编程中的let与(())
- 模仿github网页前端HTML,仿github404页面特效
- Linux下基于socket多线程并发通信的实现
- CDE桌面环境中自动启动应用程序图形界面
- golang mysql demo
- Codeforces 1187E - Tree Painting(树上所有节点的儿子数量和最大)
- C++_类和对象_C++运算符重载_加号运算符重载_实现两个对象相加_对象和int类型相加_通过成员函数重载+号_全局函数重载+号_以及重载_运算符重载函数实现---C++语言工作笔记055
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- 如何选择B2C电商仓储系统?
- HttpHandler和ashx使用Session 出现未初始化异常
- echarts无数据时显示无数据_无服务器数据库竞技,哪家云服务落伍了?
- 校准 Linux 服务器的时间
- 图像生成质量fid、inception score、KID计算
- 美元MogaFX指数介绍(二)
- VR系列——Oculus Audio sdk文档:一、虚拟现实音频技术简介(5)——环境建模
- 修改串口服务器,串口虚拟化 | 串口服务器Nport 5630 设置
- 双活数据中心负载均衡理解
- 14款S400升级20款S450外观套件
- python pip install fitter 失败解决方案
- java如何创建一个文本框_如何创建绑定到对象的文本框
热门文章
- keepalived and heartbeat
- [ObjectiveC]NSDATA, NSDICTIONARY, NSSTRING互转
- 快过年了,为过完年跳槽的人准备一份面试题
- Docker 宿主机定时清除容器的运行日志
- java Collection-Map 之 TreeMap
- php导出excel(xls或xlsx)(解决长数字显示问题)
- TSQL 聚合函数忽略NULL值
- Android--通话录音
- 前端(移动端)开发利器Chrome Developer Tools秘籍(下)
- hdu5185 dp:和为n且满足后一项是前一项或者+1的数列个数