Struts2无法在可执行的战争中初始化Dispatcher - 嵌入式Tomcat

时间:2017-12-11 19:46:14

标签: spring-boot struts2 embedded-tomcat-8 xwork

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

示例项目here

错误:

  

2017-12-11 10:58:24.527 ERROR 10296 --- [nio-8080-exec-1] o.apache.struts2.dispatcher.Dispatcher:Dispatcher初始化失败

     

com.opensymphony.xwork2.config.ConfigurationException:无法加载配置。           在com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70)〜[xwork-core-2.3.34.jar!/:2.3.34]           在org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:978)〜[struts2-core-2.3.34.jar!/:2.3.34]           在org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:446)〜[struts2-core-2.3.34.jar!/:2.3.34]           在org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:490)〜[struts2-core-2.3.34.jar!/:2.3.34]           在org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)[struts2-core-2.3.34.jar!/:2.3.34]           在org.apache.struts2.dispatcher.ng.servlet.StrutsServlet.init(StrutsServlet.java:54)[struts2-core-2.3.34.jar!/:2.3.34]           在org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)[tomcat-embed-core-8.5.14.jar!/:8.5.14]           在org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:795)[tomcat-embed-core-8.5.14.jar!/:8.5.14]           在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]           在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)[tomcat-embed-core-8.5.14.jar!/:8.5.14]           在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)[tomcat-embed-core-8.5.14.jar!/:8.5.14]           在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)[tomcat-embed-core-8.5.14.jar!/:8.5.14]           在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)[tomcat-embed-core-8.5.14.jar!/:8.5.14]           在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]           在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1455)[tomcat-embed-core-8.5.14.jar!/:8.5.14]           在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]           在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)[tomcat-embed-core-8.5.14.jar!/:8.5.14]           在java.lang.Thread.run(Thread.java:748)[na:1.8.0_131]   引起:com.opensymphony.xwork2.config.ConfigurationException:无法加载bean:type:class:com.opensymphony.xwork2.ObjectFactory           在com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:247)〜[xwork-core-2.3.34.jar!/:2.3.34]           在org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:102)〜[struts2-core-2.3.34.jar!/:2.3.34]           在com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:240)〜[xwork-core-2.3.34.jar!/:2.3.34]           在com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)〜[xwork-core-2.3.34.jar!/:2.3.34]           ...省略了23个常见帧   引起:com.opensymphony.xwork2.config.ConfigurationException:Bean类型类com.opensymphony.xwork2.ObjectFactory,名称为struts,已由bean加载 - jar:file:/ C:/ workspaces / workspace / pessoal / spring-启动Struts2的/目标/弹簧引导struts2-0.0.1-SNAPSHOT.war /WEB-INF/lib/struts2-core-2.3.34.jar /struts-default.xml:!65:72           在com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:231)〜[xwork-core-2.3.34.jar!/:2.3.34]           ...省略了26个常见帧

我的相关内容:

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 的bean,并且类型为 com.opensymphony.xwork2.ObjectFactory

拜托,有人有任何想法吗?

2 个答案:

答案 0 :(得分:1)

我找到了解决方案。

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

  

Bean类型com.opensymphony.xwork2.ObjectFactory,名称为spring已经被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的 - 弹簧 - 插件-2.3.34.jar /struts-plugin.xml:29:132

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

答案 1 :(得分:0)

要从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页面上找到有关Usage的更多信息。

  

66.2打包可执行jar和war文件

     

spring-boot-maven-plugin之后pom.xml已包含spring-boot:repackage   将自动尝试重写存档以使其可执行   使用<?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> 目标。你应该配置你的   使用通常的方法构建 jar war (视情况而定)   包装要素:

package
     

您的现有存档将在Spring期间通过Spring Boot进行增强   Main-Class阶段。您要启动的主类可以是   使用配置选项指定,或添加public static void main(String[] args)   以通常的方式归属于清单。如果你没有指定   插件的主类将搜索带有的类   $ mvn package $ java -jar target/mymodule-0.0.1-SNAPSHOT.jar 方法。

     

要构建和运行项目工件,您可以键入以下内容:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
        <group android:id="@+id/group_speed" android:checkableBehavior="single" >
            <item android:id="@+id/slow" android:title="@string/speed_slow"/>
            <item android:id="@+id/normal" android:title="@string/speed_normal"/>
            <item android:id="@+id/fast" android:title="@string/speed_fast" />
        </group>
        <group android:id="@+id/group_info" android:checkableBehavior="none">
            <item android:id="@+id/help" android:title="Help"/>
            <item android:id="@+id/about" android:title="About"/>
        </group>
</menu>