我一直在尝试将 Struts 2 与零配置、Spring、Hibernate 和 Maven 集成。
但是,我认为在集成中一定有一些我缺少的东西,它一定与 Maven 的 Pom.xml
的配置有关:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.1.8.1</version>
</dependency>
<!-- Struts 2 + Spring plugins -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.1.8.1</version>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</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>
<!-- Hibernate core -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm-all</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.7.Final</version>
</dependency>
<!-- Hibernate core library dependency start -->
<!-- Hibernate core library dependency end -->
<!-- Hibernate query library dependency start -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.1.8.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
当我使用此配置时,它给了我一个错误
java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.finder.UrlSet.<init>(Lcom/opensymphony/xwork2/util/finder/ClassLoaderInterface;Ljava/util/Set;)V
我使用的依赖项不是 opensymphony XWork libaray
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.8</version>
</dependency>
它向我抛出了这个异常
Caused by: Unable to load configuration. - [unknown location]
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
最佳答案
当您升级Struts2版本时,您必须将应用程序所需的库更新到目标版本。每个 Struts2 发行版都在 lib
文件夹中提供了与该发行版版本兼容的相应库集。如果您使用 Maven 来解析和获取依赖项,您应该考虑 Artifact struts2-core
。
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.8</version>
</dependency>
<小时/>
它将获取此 Artifact 所需的所有依赖项、其他依赖项,例如您需要单独添加的插件。使用针对插件的相同版本。例如添加一个约定插件使用
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.8</version>
</dependency>
<小时/>
我选择的目标版本号是 2.3.8,但它可能与其他功能(即 Hibernate 和 Spring)不兼容,这些版本需要通过向依赖项添加相应的 Artifact 来单独升级。
最后也是最耗时的是更改项目的源代码、根据较新的 DTD 更新配置文件、API 更改、修复弃用内容。为此,请考虑阅读 release notes .
<小时/>另请参阅开发 Maven 项目的示例:Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application .
关于java - xwork和xwork-core的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17821441/