Struts2 Spring2 整合_Java实例教程_Java_软件编程

来源:百度文库 编辑:神马文学网 时间:2024/04/29 15:47:43
Struts2  Spring2  整合
要实现与Struts与Spring的集成有如下几步
1.    将struts2-spring-plugin-2.0.8.jar文件包含到我们的应用中,放到WEB-INF/lib目录下面即可.
2.配置Spring监听器.
将下面一段话插入到web.xml文件中,同时将spring.jar文件放到WEB-INF/lib目录下面。


org.springframework.web.context.ContextLoaderListener


3.利用Spring配置文件来注册对象
(1)这里有一点得注意:使用spring配置文件注册action对象,
中使用 ID 来定义,class=“action包引用类名”
例如:
abstract="false" lazy-init="default" autowire="default">

(2)     在struts.xml文件中:中的class为spring
配置文件中的 ID

/result.jsp

这之后的步骤就和我们使用单独使用Spring一样了,现在我们就已经完成了 Struts与Spring的集成了。这里有一点要注意的是,我们还要指定Spring配置文件的地点,默认情况下,容器会到WEB-INF目录下面去寻 找applicationContext.xml文件。如果我们想指定别的地方的配置文件或者指定多个配置文件,可以通过在web.xml文件中定义 context-param元素来指定,如下所示:

contextConfigLocation

/WEB-INF/applicationContext.xml,classpath:applicationContext-*.xml


上面一段话表示WEB-INF下面的applicationContext.xml文件以及classpath下面的所有匹配applicationContext-*.xml模式的文件都会作为Spring配置文件被装载
web.xml 的源代码清单

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">


contextConfigLocation
/WEB-INF/applicationContext*.xml,classpath*:applicationContext*.xml


struts2

org.apache.struts2.dispatcher.FilterDispatcher



struts2
/*


index.jsp



org.springframework.web.context.ContextLoaderListener



struts.xml

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

value="type" />



/HelloPOJO.jsp



applicationContext.xml

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


Spring Struts 2 注入



文章出处:http://www.diybl.com/course/3_program/java/javashl/2008419/110506.html