1.添加web.xml文件
在web工程的webapp/WEB-INF/目录下添加xml文件
含有基本配置信息的web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="/2001/XMLSchema-instance"xmlns="/xml/ns/javaee"xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><!--项目名称--><display-name>crm-parent</display-name><!--启动项目,使用项目指定路径访问时默认打开的页面--><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
2.添加Struts2的配置文件struts.xml
1.在web工程的resource目录下(struts.xml文件的默认存放位置)添加Struts2的配置文件struts.xml(名称是固定的,不能改动)
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""/dtds/struts-2.3.dtd"><struts><!--将struts的有关对象创建的功能移交给spirng管理--><constant name="struts.objectFactory" value="spring"></constant><!--配置项目的package信息--><package name="crm" extends="struts-default"><!--配置action信息--><action name="testAction_*" class="textAction" method="{1}"><result name="save">/WEB-INF/page/system/textAdd.jsp</result></action></package></struts>
2.将Struts2拦截器配置到web.xml文件中
<!-- 配置struts2 --><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><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher></filter-mapping>
3.添加spring的配置文件applicationContext.xml
1.添加spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xmlns:aop="/schema/aop"xmlns:tx="/schema/tx"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd/schema/context/schema/context/spring-context.xsd/schema/aop/schema/aop/spring-aop.xsd/schema/tx /schema/tx/spring-tx.xsd"><!-- 1.使用扫描方式将类加载进spring容器 --><context:component-scan base-package="com.itheima.crm"></context:component-scan><!-- 2.整合数据库驱动c3p0(含有数据库连接信息) --><bean id="dataSource" class="com.mchange.boPooledDataSource" destroy-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver" /><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itheima1128elec?useUnicode=true&characterEncoding=utf8&autoReconnect=true"/><property name="user" value="root" /><property name="password" value="shoheh" /></bean><!-- 3.spring整合hibernate --><bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><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><!-- 4.合并hibernate的配置文件hibernate.cfg.xml --><property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property></bean><!-- 5.配置事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><!-- 6.开启声明式事务处理 --><tx:annotation-driven transaction-manager="transactionManager"/></beans>
2.将spring配置到web.xml中
<!-- 配置spring监听器 --><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>
4.创建hibernate的配置文件
hibernate.cfg.xml配置文件中配置持久化类及其映射文件的路径