1.jar包整合
Struts2:
必备包+struts与spring整合的插件包
基本jar包(11个)
从struts-2.3.7-all.zip资源包中获取apps目录下对应jar包
apps\struts2-blank\WEB-INF\lib\*.jar
相关jar包(1+2个)
struts2整合Spring(需要使用)
struts2-spring-plugin-2.3.7.jar
struts2整合Ajax(通常需要使用)
struts2-json-plugin-2.3.7.jar
struts2使用注解开发(根据需求开启)
struts2-convention-plugin-2.3.7.jar
配置文件:
struts.xml
web.xml
web.xml文件中要添加struts2的核心过滤器,否则无法使 struts拦截请求 Spring3:
核心jar包(4个)
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
日志包(2个)
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
AOP包(4个)
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
JDBC包(2个)
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
整合ORM框架(1个)
spring-orm-3.2.0.RELEASE.jar
WEB集成(1个)
spring-web-3.2.0.RELEASE.jar
配置文件:
applicationContext.xml
web.xml文件中添加spring的IoC容器加载监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> log4j.properties log4j.rootLogger=info,console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n Hibernate 核心jar包(1个) hibernate3.jar 必须的jar包(6个) lib\required\目录下的所有包 antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar javassist-3.12.0.GA.jar jta-1.1.jar slf4j-api-1.6.1.jar jpa的jar包(1个) hibernate-jpa-2.0-api-1.0.1.Final.jar slf4j整合log4j的jar包(1个) (log4j在spring中已导入) slf4j-log4j12-1.7.2.jar 配置: hibernate.cfg.xml Model.hbm.xml 其他Jar包 数据库连接包 log4j包(已经导入) 2.spring如何整合Web 使用监听器完成spring环境进入web容器,当web容器启动时,监听器将启动,加载配置中的配置 文件,并将spring上下文对象加载到ServletContext范围下 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> 3.sturts2与spring整合 A.调试struts2,保障其正常工作 B:将struts与spring整合
在struts中使用spring的Bean的方式共有4中 I.直接创建ApplicationContext对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserEbi userEbi = (UserEbi) ctx.getBean("userEbi"); 优点:容易掌握 缺点:每个操作都要重新加载配置文件,工作量巨大,并且加载的配置只能使用一次 解决方案一:将该对象设置为类级别属性或更高级别的属性 II.将ApplicationContext对象放置到更大的范围中,例如web环境中 依赖web.xml文件中的监听器完成该工作 ServletContext sc = ServletActionContext.getServletContext(); WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc); 优点:解决了ApplicationContext对象的多次加载问题 缺点:操作过于繁琐 解决方案二:能不能不写该操作 III.使用struts2与spring整合的插件包 基于插件包中的配置<constant name="struts.objectFactory" value="spring" /> 此时Struts对象的属性可以使用spring的自动装配模式,默认按照name装配 struts.objectFactory.spring.autoWire = name 此时struts的对象工厂由Spring进行控制,需要的属性spring自动装配/注入 优点:操作模式及其简洁 缺点:目前Struts的Action类的对象是由struts控制创建的 解决方案三:将struts的对象交由spring 管理 IV.将Action配置为Bean 将Action类配置成Spring的Bean struts.xml中修改Action类的class属性为Bean的名称,该名称称为伪类名 此时Spring管理Action类 优点:所有对象集中在spring的IoC容器中进行管理 缺点:没有缺点 注意:ActionBean配置时,千万要记得配置scope=”prototype” 4.hibernate3与spring整合 整合Hibernate有两种格式 引入式整合(少用) 独立整合(主流) 引入式整合: A.DAO类的制作要求继承HibernateDaoSupport 该类需要注入SessionFactory,注入setter方法已经由父类定义,无需手工书写 B.在DAO类中注入SessionFactory,首先声明Bean <!-- Dao --> <bean id="userDao" class="cn.itcast.ssh.user.dao.impl.UserImpl"> <!--注入SessionFactory --> <property name="sessionFactory" ref=" sessionFactory "/> </bean> C.声明SessionFactory对应的Bean <!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> </bean> D.Hibernate模板默认没有自动的事务提交,因此需要手工添加事务,使用注解格式为业务层接口@Transactional public interface UserEbi { public void add(UserModel um); } E.使用注解事务必须开启注释事务支持 applicationContext.xml文件中开启该注解驱动支持 <!-- 开启注释事务驱动 --> <tx:annotation-driven transaction-manager=" txManager "/> F.需要为注解驱动支持指定具体的事务管理器,使用Hibernate专用的事务管理器 <!-- txManager --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> 独立整合方式 将hibernate.cfg.xml中的配置信息搬家到applicationContext.xml文件中 <!-- SessionFactory -->
<!-- 独立整合 -->
<!-- 将hibernate.cfg.xml文件中的配置信息搬家到这里 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 数据库连接的配置 -->
<property name="dataSource" ref="dataSource"/>
<!-- 可选配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernte.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernte.show_sql">true</prop>
<prop key="hibernte.format_sql">true</prop>
</props>
</property>
<!-- 资源注册 -->
<property name="mappingResources">
<list>
<value>cn/itcast/ssh/user/vo/UserModel.hbm.xml</value>
</list>
</property>
</bean>
<!-- 将数据库连接的配置转换为dataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssh"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean>
独立整合常用资源注册的格式
格式二:
<property name="mappingDirectoryLocations">
<list>
<value>classpath:cn/itcast/ssh</value>
</list>
</property>
格式三:推荐使用此格式(通配符格式)
<property name="mappingLocations">
<list>
<value>classpath:cn/itcast/ssh/*/vo/*Model.hbm.xml</value> </list>
</property>