Struts 2 无法在可执行文件中初始化 Dispatcher war - 嵌入式 Tomcat

Struts 2 fails to initialize Dispatcher in executable war - Embedded Tomcat

我正在将 Web 应用程序从 JBoss 迁移到嵌入式 Tomcat。它适用于 intellij,但是当我 运行 带有 java -jar myapplication.warwar 文件时,它显示以下错误。谷歌搜索后我找不到任何解决方案。这个问题好像我有两个 struts2-core 罐子或一些依赖项冲突但我只有一个罐子。

示例项目here

错误:

 2017-12-11 10:58:24.527 ERROR 10296 --- [nio-8080-exec-1] o.apache.struts2.dispatcher.Dispatcher   : Dispatcher initialization failed
 
 com.opensymphony.xwork2.config.ConfigurationException: Unable to load configuration.
         at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70) ~[xwork-core-2.3.34.jar!/:2.3.34]
         at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:978) ~[struts2-core-2.3.34.jar!/:2.3.34]
         at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:446) ~[struts2-core-2.3.34.jar!/:2.3.34]
         at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:490) ~[struts2-core-2.3.34.jar!/:2.3.34]
         at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74) [struts2-core-2.3.34.jar!/:2.3.34]
         at org.apache.struts2.dispatcher.ng.servlet.StrutsServlet.init(StrutsServlet.java:54) [struts2-core-2.3.34.jar!/:2.3.34]
         at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:795) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131]
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131]
         at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.14.jar!/:8.5.14]
         at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]
 Caused by: com.opensymphony.xwork2.config.ConfigurationException: Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory
         at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:247) ~[xwork-core-2.3.34.jar!/:2.3.34]
         at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:102) ~[struts2-core-2.3.34.jar!/:2.3.34]
         at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:240) ~[xwork-core-2.3.34.jar!/:2.3.34]
         at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67) ~[xwork-core-2.3.34.jar!/:2.3.34]
         ... 23 common frames omitted
 Caused by: com.opensymphony.xwork2.config.ConfigurationException: Bean type class com.opensymphony.xwork2.ObjectFactory with the name struts has already been loaded by bean - jar:file:/C:/workspaces/workspace/pessoal/spring-boot-struts2/target/spring-boot-struts2-0.0.1-SNAPSHOT.war!/WEB-INF/lib/struts2-core-2.3.34.jar!/struts-default.xml:65:72
         at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:231) ~[xwork-core-2.3.34.jar!/:2.3.34]
         ... 26 common frames omitted

我的相关资料:

StrutsServlet:

@WebServlet(urlPatterns = { "*.do" })
public class SBSStrutsServlet extends StrutsServlet {

    private static final long serialVersionUID = 4919365268043339311L;

}

Application.java:

@SpringBootApplication
@ServletComponentScan
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

struts.xml:

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.objectFactory" value="spring"/>

    <package name="sbs" namespace="/sbs" extends="struts-default">
        <action name="welcome" class="com.gbarbosa.sbs.controller.HelloWorldAction">
            <result name="success">welcome.jsp</result>
        </action>
    </package>
</struts>

WEB-INF\lib:

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
classmate-1.3.3.jar
commons-fileupload-1.3.2.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
ecj-4.5.1.jar
freemarker-2.3.26-incubating.jar
hibernate-validator-5.3.5.Final.jar
jackson-annotations-2.8.0.jar
jackson-core-2.8.8.jar
jackson-databind-2.8.8.jar
javassist-3.11.0.GA.jar
jboss-logging-3.3.1.Final.jar
jcl-over-slf4j-1.7.25.jar
jstl-1.2.jar
jul-to-slf4j-1.7.25.jar
log4j-api-2.7.jar
log4j-core-2.8.2.jar
log4j-over-slf4j-1.7.25.jar
logback-classic-1.1.11.jar
logback-core-1.1.11.jar
ognl-3.0.21.jar
slf4j-api-1.7.25.jar
snakeyaml-1.17.jar
spring-aop-4.3.8.RELEASE.jar
spring-beans-4.3.8.RELEASE.jar
spring-boot-1.5.3.RELEASE.jar
spring-boot-autoconfigure-1.5.3.RELEASE.jar
spring-boot-starter-1.5.3.RELEASE.jar
spring-boot-starter-logging-1.5.3.RELEASE.jar
spring-boot-starter-web-1.5.3.RELEASE.jar
spring-context-4.3.8.RELEASE.jar
spring-core-4.3.8.RELEASE.jar
spring-expression-4.3.8.RELEASE.jar
spring-web-4.3.8.RELEASE.jar
spring-webmvc-4.3.8.RELEASE.jar
struts2-core-2.3.34.jar
struts2-spring-plugin-2.3.34.jar
tomcat-embed-core-8.5.14.jar
tomcat-embed-el-8.5.14.jar
tomcat-embed-jasper-8.5.9.jar
validation-api-1.1.0.Final.jar
xwork-core-2.3.34.jar

WAR 最终结构:

META-INF
org
sbs
WEB-INF

我真的坚持这个和 IntelliJ 它工作正常。我不知道为什么 Struts 已经创建了一个名为 struts 且类型为 com.opensymphony.xwork2.ObjectFactory.

的 bean

拜托,有人有什么想法吗?

要从 maven 项目生成可执行 jar 或 war,您应该使用

   <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <executable>true</executable>
            <classifier>boot</classifier>
        </configuration>
    </plugin>

允许您重新打包 war 文件以使其可启动。您可以在 Spring Boot Maven plugin on this page or on the Usage 页面找到更多信息。

66.2 Packaging executable jar and war files

Once spring-boot-maven-plugin has been included in your pom.xml it will automatically attempt to rewrite archives to make them executable using the spring-boot:repackage goal. You should configure your project to build a jar or war (as appropriate) using the usual packaging element:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- ... -->
    <packaging>jar</packaging>
    <!-- ... -->
</project>

Your existing archive will be enhanced by Spring Boot during the package phase. The main class that you want to launch can either be specified using a configuration option, or by adding a Main-Class attribute to the manifest in the usual way. If you don’t specify a main class the plugin will search for a class with a public static void main(String[] args) method.

To build and run a project artifact, you can type the following:

$ mvn package
$ java -jar target/mymodule-0.0.1-SNAPSHOT.jar

我找到了解决方案。

虽然它看起来像一个重复的 jar 问题,但事实并非如此。问题是:tomcat 容器为同一个文件提供两个不同的路径。不同的前缀路径 jar:file:/jar:war:file:/.

Bean type class com.opensymphony.xwork2.ObjectFactory with the name spring has already been loaded by bean - jar:file:/C:/target/spring-boot-struts2-0.0.1-SNAPSHOT.war!/WEB-INF/lib/struts2-spring-plugin-2.3.34.jar!/struts-plugin.xml:29:132 - bean - jar:war:file:/C:/target/spring-boot-struts2-0.0.1-SNAPSHOT.war*/WEB-INF/lib/struts2-spring-plugin-2.3.34.jar!/struts-plugin.xml:29:132

为了解决这个问题,我只是将 struts2-core 和 struts2-spring-plugin 范围从编译更改为提供。 spring-boot-maven-plugin 将这些文件放在 WEB-INF/lib-provided 下,然后当我 运行 使用 java -jar myapplication.war 时,这些 jar 在 [=30] 中提供=]时间。