整合原理:
Spring容器可以帮助struts管理action,帮助Hibernate管理SessionFactory。session的维护以及AOP事务都可以交给Spring。
1 导包:
hibernate包:hibernate/lib/request
hibernate/lib/jpa
数据库驱动
structs包:structs整合Spring包,一旦导入,struts就会在启动的时候寻找spring容器
spring包:基本的4+2包(core beans context expression logging log4j),整合web的包(Spring-web),整合aop的四个,整个jdbc事务的4个,整个junit4测试的test包
2:整合:
单独配置Spring容器
创建配置文件并导入约束(4个 beans|context|aop|tx):
src目录下创建: applicationCOntext.xml。导入约束时候点mxl的design项目,右键edit namespace,选择添加
其实可以直接粘
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
</beans>
在web.ximl文档中:
创建让Spring随web启动而创建的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
配置spring配置文件位置参数
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
单独配置Struts2:
创建struts.xml:导约束
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="crm" namespace="/" extends="struts-default">
<action name="UserAction_*" class="cn.itcast.web.action.UserAction" method="{1}">
<result name="success">success.jsp</result>
</action>
</package>
</struts>
在web.xml中配置Struts核心过滤器:
<!-- struts核心过滤器 -->
<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>
3structs和Spring整合(导入struts2-spring-plugin-2.3.24包):
配置常量在structs.xml文件中:
将action的创建交给spring容器:<constant name="struts.objectFactory" value="spring"></constant>
整合:
在applicationContext.xml中:
<bean name="userAction" class="cn.itcast.web.action.UserAction" scope="prototype"></bean>
在action里的class属性上填写spring中action对象的bean 的name的名。
3单独配置hibernate:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库url -->
<property name="hibernate.connection.url">jdbc:mysql:///user</property>
<!-- 数据库连接用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 数据库方言
注意: MYSQL在选择方言时,请选择最短的方言.
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 将hibernate生成的sql语句打印到控制台 -->
<property name="hibernate.show_sql">true</property>
<!-- 将hibernate生成的sql语句格式化(语法缩进) -->
<property name="hibernate.format_sql">true</property>
<!--
自动导出表结构. 自动建表
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 引入实体配置文件 -->
<mapping resource="cn/itcast/domain/Customer.hbm.xml" />
<mapping resource="cn/itcast/domain/LinkMan.hbm.xml" />
<mapping resource="cn/itcast/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>