前言
到目前为止,Struts2、Hibernate、Spring框架都过了一遍了。也写过了Spring怎么与Struts2整合,Spring与Hibernate整合…本博文主要讲解SSH的整合
整合步骤:
- 1) 引入SSH Jar文件
- Struts 核心jar
- Hibernate 核心jar
- Spring
- Core 核心功能
- Web 对web模块支持
- Aop aop支持
- Orm 对hibernate支持
- Jdbc/tx jdbc支持包、事务相关包
- 2)配置文件
- Web.xml
- 初始化struts功能、spring容器
- Struts.xml 配置请求路径与映射action的关系
- Spring.xml IOC容器配置
- bean-base.xml 【公用信息】
- bean-service.xml
- bean-dao.xml
- bean-action.xml
- Web.xml
需求:员工与部门之间的关系。当操作员工的时候,可以得到员工所在的部门
引入jar文件
配置文件
web.xml配置文件
初始化struts功能、spring容器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--初始化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>
<!--初始化Spring容器-->
<!-- 2. spring 配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/bean*</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Struts.xml
配置请求路径与映射action的关系【记得继承着struts-default】
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="aaa" extends="struts-default">
</package>
</struts>
SpringIOC容器配置
该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:tx="http://www.springframework.org/schema/tx"
xmlns:aop&