spring学习笔记15--Spring2.x+Hibernate3.x +Struts1.x整合开发(3)struts再次尾随sh
来源:互联网 发布:谷歌关键词优化 编辑:程序博客网 时间:2024/04/30 14:10
上一章介绍了一.action未交给spring管理
本章介绍
二、action交给spring管理(实现依赖注入,简化action控制端代码)
这样action对象就不是有struts容器创建,而是由spring容器创建、
(1)将action类配置到spring配置文件中的一对bean标签中
注意:把action交给spring管理后,可以使用依赖注入在action中注入业务层的bean,确保action的path属性值与bean的名称相同
spring中由于id属相不能含有/所以使用name属性
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- 配置数据源 --><bean id="dataSourceOrcl" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/><property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/><property name="username" value="websb"/><property name="password" value="ddit"/><!-- 连接池启动时的初始值 --><property name="initialSize" value="1"/><!-- 连接池的最大连接数 --><property name="maxActive" value="500"/><!-- 最大空闲值,当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,直到减少到MaxIdle为止 --><property name="maxIdle" value="2"/><!-- 最小空闲值,当空闲的连接数少于两个值时,连接池就会预申请一些连接数,以免洪峰来时来不及申请 --><property name="minIdle" value="1"/></bean><!--hibernate二级缓存的配置 只存在一个(单例) --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSourceOrcl"/><!-- hibernate映射源数据 可以多个--><property name="mappingResources"><list><value>cn/itcast/bean/Person.hbm.xml</value></list></property><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.OracleDialect<!--方言 -->hibernate.hbm2ddl.auto=update <!-- 是否根据映射源文件生成表结构 -->hibernate.show_sql=false <!-- 是否打印sql -->hibernate.format_sql=false<!-- 是否格式化sql --></value></property></bean><!-- 配置事务管理 :spring提供的hibernate事务管理器 --><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><!--只要是通过sessionFactory创建的事务都会纳入这个管理器进行管理 --><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 采用@Transaction注解方式使用事务transaction-manager:指定事务管理器 --><tx:annotation-driven transaction-manager="txManager"/><context:annotation-config/><bean id="personService" class="cn.itcast.service.imp.PersonServiceBean"></bean><!--将struts中的action交给spring,以至于实现依赖注入 --><bean name ="/person/list" class="cn.itcast.web.action.PersonSpringAction"></bean></beans>
(2)在struts配置文件中添加进spring的请求控制器
该请求控制器会先根据action的path属性值到spring容器中寻找跟改属性值同名的bean,如果寻找到即使用该bean处理用户请求
如果spring请求控制器在spring容器中没有找到相应的bean,则将请求转给struts,struts框架找path,进type
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><!--注意标签顺序: The content of element type "struts-config" must match "(display-name?,description?,data-sources?,form- beans?,global-exceptions?,global-forwards?,action-mappings?,controller?,message-resources*,plug-in*)". --><struts-config> <action-mappings> <action path="/person/list" validate="false"> <forward name="list" path="/WEB-INF/page/personlist.jsp"></forward> </action> </action-mappings> <!-- 配置由spring提供的控制器 --> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/> </controller></struts-config>
spring控制器运行过程:
(3)action类得意简化,是的struts与spring容器进行解耦
package cn.itcast.web.action;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import cn.itcast.service.PersonService;public class PersonSpringAction extends Action {/** * 注解注入:先按对象名称匹配,不行再按类别匹配 */@Resource PersonService personService;public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {/** * 如果action没有交给spring管理,可以通过下面语句从appliaction域中获取spring容器 *//*WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());PersonService personService = (PersonService)ctx.getBean("personService");request.setAttribute("persons", personService.getPersons());return mapping.findForward("list");*//** * 将action交给spring容器管理,是的struts与spring解耦 */request.setAttribute("persons",personService.getPersons());return mapping.findForward("list");}}
- spring学习笔记15--Spring2.x+Hibernate3.x +Struts1.x整合开发(3)struts再次尾随sh
- spring学习笔记14--Spring2.x+Hibernate3.x +Struts1.x整合开发(2)struts尾随sh而至
- spring学习笔记13--Spring2.x+Hibernate3.x +Struts1.x整合开发(1)sh先行组合
- spring学习笔记16--Spring2.x+Hibernate3.x +Struts1.x整合开发(4)-----Hibernate3.x配置二级缓存
- spring学习笔记17--Spring2.x+Hibernate3.x +Struts1.x整合开发(4)-----Spring提供的CharacterEncoding和OpenSessionInV
- Struts1.x Spring2.x Hibernate3.x DWR2.x整合工具文档v1.00
- Struts1.x Spring2.x Hibernate3.x DWR2.x 整合工具文档
- Spring2.0+Struts1.x+Hibernate3.2集成
- struts1.x +Spring2.0
- hibernate3.x学习笔记
- Spring与Struts1.x整合
- spring与struts1.x整合
- struts1.x和spring整合
- Struts1.x 学习笔记
- struts1.x学习笔记
- Struts1.x + hibernate3 +spring2 集成需要的库
- ssh 整合之 struts1.x 和 spring2.5的整合
- Struts2.x、Spring3.x、Hibernate3.x整合开发
- shell中while循环案例
- java编程思想-继承中的多态及相关思考
- 依赖倒置原则
- Stare only you
- android 网络编程--URL获取数据/图片
- spring学习笔记15--Spring2.x+Hibernate3.x +Struts1.x整合开发(3)struts再次尾随sh
- 树形DP
- Linux 信号signal处理机制
- 常见数组面试题
- Eclipse Debug
- tomcat配置多域名
- Virtual Ubuntu Resolution Settings
- MapReduce 模式、算法和用例
- myeclipse 启动服务时 提示“Address already in use:JVM_Bind:8080”