Spring3.2与Struts2的整合环境

一、必要的工具

其一:Eclipse以及必要的环境就不赘述了。

其二:新建一个web工程。

二、必要的包(在下面可以下载)

Struts2:

(1)struts2-core-2.3.24.jar

(2)xwork-core-2.3.24.jar

(3)ognl-3.0.6.jar

(4)javassist-3.11.0.GA.jar

(5)freemarker-2.3.22.jar

(6)commons-lang3-3.2.jar

(7)commons-io-2.2.jar

(8)commons-fileupload-1.3.1.jar

(9)struts2-spring-plugin-2.3.24.jar(这个包是用来整合spring的,一定要记得加)

spring:(ps 我用的是3.2版本)

(1)spring-aop-3.2.0.M1.jar

(2)spring-asm-3.2.0.M1.jar

(3)spring-aspects-3.2.0.M1.jar

(4)spring-beans-3.2.0.M1.jar

(5)spring-context-3.2.0.M1.jar

(6)spring-core-3.2.0.M1.jar

(7)spring-context-support-3.2.0.M1.jar

(8)spring-expression-3.2.0.M1.jar

(9)spring-tx-3.2.0.M1.jar

(10)spring-web-3.2.0.M1.jar

三、必要的配置文件

关于web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext*.xml</param-value>
 </context-param>
  
    <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>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
 
</web-app>

一定要记得配置一个Spring提供的监听器,不然执行会报错。就是已标注为红色的部分,

我的applicationContext直接放在了src下面。个人根据放置的位置具体配置!

关于Struts.xml:

这个配置文件没什么好配的,关键就是在使用的使用的时候注意两点:

第一、在struts.xml配置action时,class属性写的是bean的名称!
第二、Action对象要是多例的。

关于applicationContext.xml

在这个里面一定要记得打开组件扫描,并定义你想扫描的包,如下:

<context:component-scan base-package="com.*.*"></context:component-scan>

对于这个文件的头部一次就全配好,以后每次配的时候直接拷贝就会比较轻松:

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

最后如果你的xml在进行编辑的时候没有智能提示,单击eclipse下的windows--->preferences--->搜索xml catalog,选中做相应的修改就可以了。

     我只是个菜鸟,如果写得不好,不喜勿喷,最后希望能够帮助到少部分的人!

猜你喜欢

转载自19900524.iteye.com/blog/2231771
今日推荐