19

使用 Struts2,我们必须struts.xml在类路径中拥有,因此在 WEB-INF 下拥有它不再有效。所以我让我的项目部署的方式是将它放在下面WEB-INF/classes并包含它../struts2.xml

2 问题:

  1. 当我进行重建时,Eclipse 会清除 classes 文件夹,因此它会删除struts.xml
  2. Eclipse 没有在我的项目浏览器中显示类文件夹,因此它是一个糟糕的地方,首先粘贴配置文件。

Struts2 Eclipse 开发人员是如何做到这一点的?

4

6 回答 6

17

您可以将 struts.xml 放在源目录的根目录下,也可以设置一个额外的资源源目录并将其放在那里。Eclipse 在编译时非常乐意为您将其复制到 WEB-INF/classes 中。

于 2009-05-16T12:56:14.087 回答
2

我迟到了,我们可以在 web 应用程序的类路径中的任何目录中配置 struts.xml,但是使用 web.xml 中的过滤器配置的“config” init 参数提供位置,如下所示,如果我的 struts .xml 文件位于“/com/resources/”目录中。

   <filter>
        <filter-name>action</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
         <init-param>
            <param-name>config</param-name>
            <param-value>struts-default.xml,struts-plugin.xml,/com/resources/struts.xml</param-value>
         </init-param>
    </filter>

如果我们不提供配置 init 参数 struts2 默认取 3 个值“struts-default.xml,struts-plugin.xml,struts.xml”,你可以看到下面的 struts2 Dispatcher 类代码,它将配置这 3 个文件配置管理器。

String configPaths = (String)this.initParams.get("config");
if (configPaths == null) {
  configPaths = "struts-default.xml,struts-plugin.xml,struts.xml";
}
String[] files = configPaths.split("\\s*[,]\\s*");
for (String file : files)
  if (file.endsWith(".xml")) {
    if ("xwork.xml".equals(file))
      this.configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));
    else
      this.configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, this.servletContext));
  }
于 2014-11-15T15:01:42.987 回答
2

使用 Struts 1.2,需要将 struts-config.xml 放在类路径中(在 WEB-INF 文件夹下),但使用 Struts 2.0,它需要放在 src/main/resources 文件夹中。

在此处查看文档Struts 2 文档

我将 struts.xml 粘贴到此目录中,项目执行良好。

我的项目结构是这样的

于 2018-04-23T20:45:22.593 回答
1

我没有使用 Eclipse,所以这个答案不是特定于您的要求,但是,我使用 Maven,所以我们在一个名为“resources”的单独文件夹中拥有应用程序所需的所有“资源”,并且在构建应用程序时这些文件是自动复制到合适的地方。在 Netbeans 中,文件夹中的文件是可用的,我知道有些人使用 eclipse 具有类似的设置。

我应该指出,我们的项目是从appfuse开始的,所以大多数这些配置都是预先制作的。你可以看看它是如何在那里完成的。

于 2009-05-15T19:11:40.387 回答
0

You can place struts.xml file in src(Java Resources) packages. When the compilation process struts.xml file will generate inside the ROOT/WEB-INF/classes directory.

if you get the same error again and again better check the struts actions. check the deployed path of the application and you can find out what you want. (struts.xml file)

于 2014-04-10T09:42:20.997 回答
0

在 struts 2 项目中,struts.xml 文件与包和库一起添加到 src(Java Resources) 文件夹中。

请参考下面给出的图像。

Eclipse中的Struts 2项目目录视图

如果您想了解更多关于 struts 2 项目结构的信息,请访问此链接

注意:在 Eclipse 中,不允许将文件直接粘贴到 src 文件夹中。因此,您需要先将其粘贴到项目中的任何其他位置(例如,在“WebContent”文件夹中),然后使用移动功能将其放置在正确的位置(即“src”文件夹)。

于 2012-02-27T09:08:54.287 回答