0

我有一个通过 Maven 部署到 .war 文件的 java-servlet 项目。这在 tomcat servlet 容器中运行。因为我使用的是 Wordnet(准确地说,我使用了这个 API:http : //extjwnl.sourceforge.net/),所以我有一个所谓的 file_properties.xml 文件,其中包含“dictionary_path”的值。此值应保存存储字典的路径。当我在本地机器上测试以下值时 - 当然 - 有效:“C:\Users\XY\Documents\NetBeansProjects\project-name\src\main\resources\dict\”

在部署到战争文件并在 tomcat 中运行之后,这条路径当然不再起作用了。如何正确输入 XML 中的值?目标文件夹位于“\WEB-INF\classes”文件夹中的战争文件中。

xml 现在看起来像这样: <param name="dictionary_path" value="C:\Users\username\Documents\NetBeansProjects\projectname\src\main\resources\dict\"/>

解决方案:我们现在通过解决方法解决了这个问题:我们不再需要 XML 文件。相反,我们通过提供路径来实例化字典:

String dictPath = WordNetDictionary.class.getResource("/dict").toExternalForm();
//trim dictPath here depending on the OS
this.dictionary = Dictionary.getFileBackedInstance(dictPath);
4

2 回答 2

0

鉴于您的框架似乎需要一个文件,并且您正在打包为 .war,因此您的直接问题是。

我也许会采取以下方法。将属性文件打包到 .war 中。然后,您可以使用ClassLoader.getResourceAsStream()。这将找到与您的类一起打包的属性文件。阅读它,并将其写入指定目录。该目录应与您的 XML 配置中的目录匹配。然后,您可以使用给定的配置实例化您的框架,并且该路径将指向您编写文件的目录。

于 2013-01-16T12:41:40.450 回答
0

解决方案:我们现在通过解决方法解决了这个问题:我们不再需要 XML 文件。相反,我们通过提供路径来实例化字典:

String dictPath = WordNetDictionary.class.getResource("/dict").toExternalForm();
//trim dictPath here depending on the OS
this.dictionary = Dictionary.getFileBackedInstance(dictPath);
于 2013-01-22T09:20:31.070 回答