SSH整合:将Hibernate的session的创建交给Spring IOC容器,将Struts2的Action交给Spring IOC容器
新建一个动态web工程-->加入Spring-->加入Hibernate-->加入Struts2
web.xml
applcationContext.xml
struts.xml
hibernate.cfg.xml
*.hbm.xml
1.在 web中应用Spring
目的:在web应用程序加载成功之后,就可以使用Spring IOC容器
(1)加入jar包
除了Spring所必须的jar包之外,还有两个jar包必须加入
spring-webmvc-4.0.0.RELEASE.jar spring-web-4.0.0.RELEASE.jar
(2)在web.xml文件中,Spring提供的ContextLoaderListener启动Spring IOC容器
<context-param>
<!-- 在当前web应用的初始化参数中配置Spring配置文件的路径 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.加入Spring
(1)加入jar包
包括Spring所必须的jar包,还有配置数据源所需的jar包
(2)在类路径下创建Spring的配置文件applicationContext.xml,在配置文件中,配置数据源和Spring的声明式事务
<!-- 配置数据源,数据源的属性在外部资源文件中设置 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置Spring的声明式事务 -->
<bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="hibernateTransactionManager">
<property name="dataSource" ref="dataSource"></property>
<property name="sessionFactory" ref="localSessionFactoryBean"></property><!-- ref属性的值是加入Hibernate时配置的SessionFactory的bean的id -->
</bean>
<tx:advice id="adviceDjp" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* *.*(..))" id="pointcutDjp"/>
<aop:advisor advice-ref="adviceDjp" pointcut-ref="pointcutDjp"/>
</aop:config>
3.加入Hibernate
(1)在类路径下创建Hibernate的配置文件hibernate.cfg.xml,配置Hibernate的一些基本信息
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
(2)创建持久化类以及对应的对象-关系映射文件(.hbm.xml)
持久化类POJO对应数据库中的表,对象关系映射文件和POJO在同一个包下,命名为POJO类名.hbm.xml
(3)在Spring的配置文件applicationContext.xml中配置SessionFactory实例
<!-- 配置sessionFactory -->
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="localSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/…/…/*.hbm.xml"></property>
</bean>
4.加入Struts2
(1)加入jar包
除了Struts2所必须的jar包之外,还有一个jar包必须加入 struts2-spring-plugin-2.5.12.jar
(2)在web.xml文件中加入Struts2的Filter
<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>
(3)创建Action类,并在Spring IOC容器中配置Struts2的Action实例,可以单独再创建一个Spring的配置文件,该文件中专门用于Action类的配置
<bean class="……" id="……" scope="prototype">
……
</bean>
注意:由于Struts2的Action不是单例的,所以,在applicationContext.xml中配置时,必须修改scope属性(该属性默认singleton)
(4)在类路径下创建Struts2 的配置文件struts.xml
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="……" class="……" method="……">
<result name="……">……</result>
</action>
</package>
</struts>
注意:struts.xml中<action>的class属性的值是applicationContext.xml中对应的<bean>的id属性的值
1.web.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>
2.Spring 配置文件
<beans> <context:component-scan base-package="com.duan.*.impl"></context:component-scan> <context:property-placeholder location="classpath:jdbc.properties"/> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 配置sessionFactory,此时,要是启动web应用,会自动生成数据表 --> <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="localSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:com/duan/entities/*.hbm.xml"></property> </bean> <!-- 配置Spring的声明式事务 --> <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="hibernateTransactionManager"> <property name="dataSource" ref="dataSource"></property> <property name="sessionFactory" ref="localSessionFactoryBean"></property> </bean> <tx:advice id="adviceDjp" transaction-manager="hibernateTransactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.duan.services.*.*(..))" id="pointcutDjp"/> <aop:advisor advice-ref="adviceDjp" pointcut-ref="pointcutDjp"/> </aop:config> </beans>
<beans>
<bean class="com.duan.actions.EmployeeAction" id="employeeAction" scope="prototype">
</bean>
</beans>
3.Hibernate
<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
<hibernate-mapping> <class name="com.duan.entities.Employee" table="SSH_EMPLOYEE"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="empName" type="java.lang.String"> <column name="EMP_NAME" /> </property> <property name="empBirth" type="java.util.Date"> <column name="EMP_BIRTH" /> </property> <property name="empCreateTime" type="java.util.Date"> <column name="EMP_CREATETIME" /> </property> <!-- 单向多对一关联关系 --> <many-to-one name="department" class="com.duan.entities.Department"> <column name="DEPARTMENT_ID" /> </many-to-one> </class> </hibernate-mapping>
4.struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <interceptors> <interceptor-stack name="duanStack"> <interceptor-ref name="paramsPrepareParamsStack"> <param name="prepare.alwaysInvokePrepare">false</param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="duanStack"></default-interceptor-ref> <action name="employeeAction-*" class="employeeAction" method="{1}"> <result name="{1}">WEB-INF/views/employee-{1}.jsp</result> <result name="ajaxSuccess" type="stream"> <param name="contentType">text/html</param> <param name="inputName">inputStream</param> </result> <!-- <result name="delete" type="redirect">/employeeAction-list</result> --> <result name="saveOrUpdate" type="redirect">/employeeAction-list</result> <!-- 类型转换异常 --> <result name="input">WEB-INF/views/error.jsp</result> </action> </package> </struts>