一、 Struts 2框架整合Spring步骤
1、 复制文件。复制struts2-spring-plugin-x-x-x.jar和spring.jar到WEB-INF/lib目录下。其中的x对应了Spring的版本号。还需要复制commons-logging.jar文件到WEB-INF/lib目录下。
2、 配置struts.objectFactory属性值。在struts.properties中设置struts.objectFactory属性值:struts.objectFactory = spring
或者在XML文件中进行常量配置:
...
3、 配置Spring监听器。在web.xml文件中增加如下内容:
org.springframework.web.context.ContextLoaderListener
4、 Spring配置文件。默认情况下,Spring配置文件为applicationContext.xml,该文件需要保存在Web应用的WEB-INF目录下。内容示例如下所示:
< xml version="1.0" encoding="UTF-8" >
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
开发者实际上可以使用多个Spring配置文件,在web.xml中进行下列设置,从而使Spring的ApplicationContext通过匹配所给定模式的文件来初始化对象:
contextConfigLocation
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
5、 修改Struts配置文件。Struts 2框架整合Spring框架,需要在Struts配置文件中有所改变,下面是一个示例:
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
foo.ftl
bar.ftl
该配置文件中定义了两个Action配置:foo是一个标准的Struts 2框架Action配置,指定了Action实现类为com.acme.Foo;bar对应的class并不存在,那么框架将在Spring配置文件中查找id属性为“bar”的定义,该配置文件如下所示:
< xml version="1.0" encoding="UTF-8" >
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
...
相关阅读: