1.struts2整合Spring
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 整合配置:struts -->
<filter>
<filter-name>strut2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 如果struts.xml在src的根目录下则不用配置,不在就要做如下配置: -->
<init-param>
<param-name>config</param-name><!-- config名不能变 -->
<param-value>struts-default.xml,struts-plugin.xml,config/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>strut2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 整合配置:spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 如果applicationContext.xml文件如果在在web_info目录下则不用配置 -->
<context-param>
<param-name>contextConfigLocation</param-name><!-- contextConfigLocation名不能改 -->
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
</web-app>
-----------------------------------------------------------------------------
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="qq" extends="struts-default">
<action name="user" class="userController">
<result name="list" type="dispatcher">WEB-INF/per/list.jsp</result><!-- 默认为转发 -->
<result name="add">WEB-INF/per/add.jsp</result>
<!-- struts中默认为转发即type="dispatcher",重定向为 redirect -->
<result name="update">WEB-INF/per/update.jsp</result>
<result name="success" type="redirect">user_list</result>
</action>
</package>
</struts>
--------------------------------------------------------------------------------
applicationContext.xml
<bean id="userController" class="com.hw.dao.impl.StudentDaoImpl"></bean>
2.Spring整合hibernate(完全整合)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 分开整 合hibernate, configLocation名不能变,只需要
<property name="configLocation" value="classpath:config/hibernate.cfg.xml"/>即操作数据库交给
hibernate -->
<!-- spring 完全整合hibernate,整个工程 不需要 hibernate.cfg.xml-->
<property name="dataSource" ref="dataSource"></property>
<!-- 设置hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 配置hibernate的映射文件 -->
<property name="mappingResources">
<list>
<value>com/hw/entity/User.hbm.xml</value>
</list>
<!-- <property name="mappingDirectoryLocations">
<list>
<!-- spring容器会去该包或者子包下搜素所有的映射文件
<value>com/hw/entity</value>
</list>-->
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
2.Spring整合hibernate(分开整合)
</bean>
<!-- 分开整 合hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- configLocation名不能变 -->
<property name="configLocation" value="classpath:config/hibernate.cfg.xml"/>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
3.事物
<!-- xml方式配置 -->
<aop:aspectj-autoproxy proxy-target-class="true" /><!-- 激活自动代理功能 -->
<!-- 定义事务通知 -->
<tx:advice id="transactionManager" transaction-manager="transactionManager">
<!-- 定义事务传播规则 -->
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
<!-- 也可以对所有方法都应用REQUIRED事务规则
<tx:method name="*" propagation="REQUIRED"/>-->
</tx:attributes>
</tx:advice>
<aop:config> <!-- 定义一个切面,并将事务通知和切面组合 -->
<aop:pointcut expression="execution(* com.dao.impl.*.*(..))" id="perform"/>
<aop:advisor advice-ref="transactionManager" pointcut-ref="perform"/>
</aop:config>
网友评论