新建 web project -->修改web.xml
<!-- 第一步 定位Spring
这一行在省略的情况下,请注意将spring配置文件applicationContext.xml定义到WEB-INF下
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml,classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动Struts过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 第二步 Spring Bean 工厂的监听器,并从 contextConfigLocation 这
个参数所指定的 XML 配置文件中创建 Bean 类
-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
注意: 在web.xml中 每一个子节点的顺序,譬如 <listener>就必须放在最后
第二步:导入Struts2的jar包 最好使用 struts-blank下的lib中的全部的包,此处还要加入另外一个包 struts2-spring-plugin.jar包,这个包是struts开放的整合spring的一个工具包
第三步:使用MyEclipse的 add capability功能导入 spring的包 此处需要选中的是spring core包和spring web包。
第四步:在src目录下创建struts.xml 内容如下:
</package>
</struts>
需要注意的是,两个<constant>的定义,在此不再详细解释。
第五步:修改applicationContext.xml :
<bean id="shello" class="qingyun.test.Hello">
<property name="message">
<value>Spring 注入</value>
</property>
</bean>
</beans>
注意两点:第一个是在<beans>节点上添加了一个属性 default-autowire="byType"
第二点是 定义<bean>的时候要注意 此处的id="shello" 要和 struts.xml中<action>定义中的class属性相一致。
ok,搞定!!