这一期开始说说SSH的整合
SSH的整合非常简单,只要写几句配置就可以实现
1.struts2和spring的整合
他们整合的目的在于把struts的action类的实例化交由spring处理
下面是struts.xml中的关于和spring整合的关键配置
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.objectFactory" value="spring" />
- <constant name="struts.devMode" value="true" />
- <!-- Other configurations -->
- </struts>
其中
<constant name="struts.objectFactory" value="spring" />
表示struts的对象交由spring托管
注意:struts.xml文件放置在classes目录下
2.spring和hibernate的整合
由于Spring本身就支持多种持久化框架,因此它内置了各种支持类来支配整合的持久化框架
下面是Spring的配置文件中整合的关键代码
- <?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/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
- <!-- SessionFactory -->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- <property name="hibernateProperties">
- <value>
- hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
- </value>
- </property>
- </bean>
- <!-- transaction -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- Other configurations -->
- </beans>
注意:Spring配置文件的路径在web.xml中配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="person" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>WEB-INF/classes/conf/application*.xml</param-value>
- </context-param>
- <!-- Other configurations -->
- </web-app>
至此,整合完毕