0

我想将 struts2 与 spring 集成。所以我在我的 web-inf/classes 文件夹中使用以下代码。

应用程序上下文.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
     <bean id="helloWorldClass" class="com.test.spring.HelloWorld" >
          <property name="message" value="Hello World!" />
     </bean>
     </beans>

struts.xml

    <!DOCTYPE struts PUBLIC 
         "-//Apache Software Foundation//DTD Struts Configuration  2.0//EN"
         "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>    
    <struts-config>
    <!-- Spring Struts plugin -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation"
        value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>
    </struts-config>
    <constant name="struts.devMode" value="false" />
    <constant name="struts.objectFactory" value="spring" />
    <package name="default" extends="struts-default">
    <action name="helloWorld" class="helloWorldClass">
        <result name="SUCCESS">/success.jsp</result>
    </action>
    </package>
    </struts>

我已经使用了这些文件和一个类,即 helloWorld,包为 com.test.spring。类中仅包含变量消息的 getter/setter 方法。

pom.xml 文件中的内容:

            <repositories>
    <repository>
        <id>jboss-snapshots</id>
        <url>http://snapshots.jboss.org/maven2</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>opensymphony-releases</id>
        <name>Opensymphony Releases</name>
<url>https://oss.sonatype.org/content/repositories/opensymphony-releases</url>
    </repository>
    <repository>
        <id>mvn-adamgent</id>
        <url>http://mvn-adamgent.googlecode.com/svn/maven/release</url>
        <name>Adam Gent Maven Repository</name>
    </repository>

    <repository>
        <id>maven2-repository.dev.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>

     <repository>
    <id>springsource-milestones</id>
    <name>SpringSource Milestones Proxy</name>
    <url>https://oss.sonatype.org/content/repositories/springsource-milestones</url>
     </repository>      
</repositories> 
     <dependencies>
      <dependency>
         <groupId>junit</groupId>
          <artifactId>junit</artifactId>
           <version>3.8.1</version>
          <scope>test</scope>
       </dependency>    
        <dependency>
      <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.1</version>
        </dependency>   
        <dependency>
          <groupId>org.apache.commons</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.3.2</version>
    </dependency>   
        <dependency>
       <groupId>commons-logging</groupId>
 <artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>   
    <dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.13</version>
   </dependency>            
   <dependency>
<groupId>opensymphony</groupId>
<artifactId>ognl</artifactId>
<version>2.6.11</version>
   </dependency>            
    <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.1.6</version>
   </dependency>           
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.1.6</version>
  </dependency>            
<dependency>
<groupId>com.opensymphony</groupId>
<artifactId>xwork</artifactId>
<version>2.1.2</version>
   </dependency>            
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.1.6</version>
 </dependency>            
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr-runtime</artifactId>
<version>3.1.3</version>
  </dependency>          
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
  </dependency>                
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
 </dependency>            
<!-- Spring framework --> 
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>2.5.6</version>
</dependency> 
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>2.5.6</version>
</dependency> 
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-struts</artifactId>
    <version>2.0.8</version>
</dependency>
     <dependency>
         <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
         <version>2.5.6</version>
       </dependency>
      </dependencies>
    <build>
        <finalName>testspring</finalName>
       </build>
    </project>

通过使用这个文件,我已经在目标的 web-inf/lib 文件夹中下载了 jar 文件。

当我在 glassfish 服务器上上传这个战争文件时,它显示以下错误消息。

部署期间发生错误:加载应用程序时出现异常:java.lang.IllegalStateException:ContainerBase.addChild:开始:org.apache.catalina.LifecycleException:java.lang.NoClassDefFoundError:无法初始化类org.springframework.web.context.ContextLoader . 有关详细信息,请参阅 server.log。

谁能帮我解决这个错误以及如何将struts2与spring集成。

提前致谢。

4

1 回答 1

0

ContextLoaderPlugin is used with Struts 1. From the DOCTYPE in your struts.xml you're using Struts 2.

See here:

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/web-integration.html#struts

For Struts 2 you should use the Struts2-Spring plugin.

于 2012-10-17T14:21:33.933 回答