我们之前用的strutes2.0和spring2.5整合作项目比较多,自己也要跟上时代的步伐,搞一下较新的,(spring最新是3.2.1了http://www.springsource.org/download/community去可以下载spring不同的版本),现在自己尝试使用struts2.3和spring3.0简单整合一下。一步一步来。
首先把struts基本的搭起来比较简单,会struts的都知道怎么办,下载jar包,我下载的是struts-2.3.4.1-lib.zip,加的基本jar包有以下,
,写struts.xml,更改web.xml,
<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>
写个action,jsp没试下。
接下来是整合spring,添加spring所要的jar包,,这个在下载的zip文件中的dist文件夹下找到,然后还要加入
commons-logging-1.1.1.jar,这时才能正确运行起来spring,添加spring的配置文件applicationContext.xml(spring3.0默认读取的路径是/WEB-INF/applicationContext.xml)
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="testAction" class="com.hls.web.TestAction"></bean> </beans>
更改web.xml添加以下内容:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果你想把applicationContext.xml放在src下面的地方,可以在web.xml下面加入以下内容:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml文件的路径(如果有很多文件,可以使用通配符)</param-value>
</context-param>
,你要是没有用到spring管理一些类的话,这时候如果使用tomcat启动,是不会报错的,运行起来也没有错,因为没有struts和spring还没有关联上,要加入一个jar包
struts2-spring-plugin-2.3.4.1.jar,然后你把struts.xml中action的class属性写成你在applicationContext.xml中定义的bean中的id,运行就可以了。
到这里是最最最简单的应用了,spring3.0系列可以使用java类来实现xml配置文件的功能,参考下文档什么的用用看。
(可以到下载我上传的这次整合的代码,http://download.csdn.net/detail/swjtueducn2/5120061)