首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何强制嵌入式Tomcat找到struts.xml文件?

如何强制嵌入式Tomcat找到struts.xml文件?
EN

Stack Overflow用户
提问于 2015-12-11 02:05:30
回答 1查看 696关注 0票数 4

我正在尝试创建一个运行在Struts2上的嵌入式Tomcat。

第一步是制作一个可以工作的纯嵌入式Tomcat。遵循本教程很容易做到这一点:

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/basic_app_embedded_tomcat/basic_app-tomcat-embedded.html#section4

现在,为了让Struts2生效,下一步是将Struts库添加到POM文件中。

代码语言:javascript
复制
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.struts.xwork</groupId>
        <artifactId>xwork-core</artifactId>
        <version>2.3.20</version>
    </dependency>       

    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.3.20</version>
    </dependency>

    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-convention-plugin</artifactId>
        <version>2.3.20</version>
    </dependency>   

小菜一碟!下一步:将Struts添加到web.xml。很简单,但是...Embedded Tomcat不加载web.xml文件!不管我做什么。解决方案:在目标JAR文件夹下创建一个名为resources的文件夹,将web.xml文件放在那里,并将以下代码添加到服务器起始类中:

代码语言:javascript
复制
tomcat.getHost().setAppBase(appBase);

StandardContext ctx = (StandardContext) tomcat.addWebapp(contextPath, appBase);
( (StandardJarScanner) ctx.getJarScanner() ).setScanAllDirectories(true);
ctx.getServletContext().setAttribute( Globals.ALT_DD_ATTR, "resources/web.xml");

tomcat.start();

这将从外部文件夹加载web.xml。工作得很好。现在我可以加载struts库了。我可以通过查看服务器日志看到这一点。所有struts加载的东西都在里面。

下一步:配置拦截器。在正常情况下,我需要将struts.xml文件放到‘`src\main\resources’文件夹(Eclipse)中。所以我试了试,但没有成功。

我的struts.xml文件只有一个拦截器标记,指向实现拦截器的类。我喜欢使用注释来配置操作,因此在此文件中没有操作标记。这个struts.xml文件和拦截器类来自我的其他系统,它工作得很好。非常简单。

问题是: struts.xml文件无法加载。我可以通过发送拦截器的intercept方法的输出并打乱struts.xml文件(将拦截器类名更改为不存在的名称、打乱标记结构等)来证明这一点。

我的问题是:如何告诉嵌入式Tomcat加载struts.xml文件,或者更准确地说,如何告诉struts引擎struts.xml的位置?

EN

回答 1

Stack Overflow用户

发布于 2016-06-13 13:23:47

尽管它不能回答OP directly.But,但创建可执行的Fat jar的预期目标可以是achieved.Future,谷歌搜索可以从this.This works with struts2中受益,我did.AFIK它将适用于任何web应用程序。

通过使用tomcat7-maven-plugin,它已经变成了一块和平的蛋糕。正如前面提到的here

代码语言:javascript
复制
 <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.1</version>
     <executions>
       <execution>
        <id>tomcat-run</id>
        <goals>
            <goal>exec-war-only</goal>
        </goals>
        <phase>package</phase>
        <configuration>
            <path>/appName</path>
            <enableNaming>false</enableNaming>
            <finalName>appName.jar</finalName>
            <charset>utf-8</charset>
         </configuration>
     </execution>
  </executions>
</plugin> 

正如here中提到的,你要么使用2.1,要么使用2.3,因为2.2有bug。根据Tomcat Docs的说法,你可以通过在插件中显式地提到所有tomcat依赖项来选择tomcat的次要版本(我还没有尝试过)。

如果你在maven中运行package goal,它将在目标文件夹中生成.jar文件。您可以使用java -jar appName.jar运行它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34208767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档