Spring,负责对象对象创建
Struts, 用Action处理请求
Spring与Struts框架整合,
关键点:让struts框架action对象的创建,交给spring完成!
Spring与Hibernate整合:
【SSH整合:
Spring与Struts
关键点: action交给spring创建!
Spring与Hibernate
关键点:sessionFactory对象交给spring创建!
】
步骤:
引入jar文件
1)引入struts.jar相关文件
2)spring-core 相关jar文件
3)spring-web 支持jar包
spring-web-3.2.5.RELEASE.jar 【Spring源码】
struts2-spring-plugin-2.3.4.1.jar 【Struts源码】
配置:
4)配置XML
struts.xml 【struts路径与action映射配置】
bean.xml 【spring ioc容器配置】
web.xml
【核心过滤器: 引入struts功能】
【初始化spring的ioc容器】
【初始化spring的ioc容器】
查看api。
4) 代码以及配置
核心:web.xml文件 关键点-->web.xml的配置(spring的配置是在spring的源码文件的文档中查找的,可以搜索context-param)
<span style="font-family:Courier New;"><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<!--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>
<span style="color:#ff6600;"><!-- 2.spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/bean*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener></span>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
</span>
struts.xml:
关键点-->配置文件中的action应该使用spring配置文件bean.xml中的userAction而不是全名
<span style="font-family:Courier New;"><?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="user" extends="struts-default">
<action name="user" class="userAction" >
<result name="success">/index.jsp</result>
</action>
</package>
</struts></span>