Struts 2整合Spring

13.2 Struts 2整合Spring Struts 2框架为整合其他技术提供了良好的可扩展性,可以通过插件的方式来实现同Spring技术的整合。 13.2.1 整合步骤 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文件中进行常量配置: <struts> <constant name=”struts.objectFactory” value=”spring” /> </struts> (3)配置Spring监听器。在web.xml文件中增加如下内容: <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> (4)Spring配置文件。默认情况下,Spring配置文件为applicationContext.xml,该文件需要保存在Web应用的WEB-INF目录下。内容示例如下所示: <?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”> <beans default-autowire=”byName”> <bean id=”personManager” class=”com.acme.PersonManager”/> </beans> 开发者实际上可以使用多个Spring配置文件,在web.xml中进行下列设置,从而使Spring的ApplicationContext通过匹配所给定模式的文件来初始化对象: <!– 用来定位Spring XML文件的上下文配置 –> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> </context-param> (5)修改Struts配置文件。Struts 2框架整合Spring框架,需要在Struts配置文件中有所改变,下面是一个示例: <!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN” “http://struts.apache.org/dtds/struts-2.0.dtd”> <struts> <include file=”struts-default.xml”/> <package [...]

你可能感兴趣的:(spring,Web,xml,框架,struts)