繁体   English   中英

如何从WEB-INF目录加载文件/属性?

[英]How to load files/properties from WEB-INF directory?

看来在我的Tapestry应用程序中,无法从WEB-INF目录或类路径加载ini文件或属性文件。

我尝试了几种不同的方法来加载文件,但没有一个起作用。

realm.setResourcePath("/WEB-INF/auth.properties");

realm.setResourcePath("classpath:wip/pages/auth.properties");

为了使用基于Shiro的tapestry-security模块,我需要加载properties / ini文件。

感谢帮助 !

尝试ServletContext.getResourceAsStream("/WEB-INF/auth.properties")ServletContext.getResourceAsStream("WEB-INF/auth.properties")

必须从servlet,servletListener等中使用ServletContext。

类路径的根是要走的路。 将文件放在src / main / resources / auth.properties中,然后使用realm.setResourcePath(“ classpath:auth.properties”);设置resourcePath

检查ExtendedPropertiesRealm和tapestry-security testapp的示例

尝试

Properties props = new Properties();
props.load(new FileInputStream(new File(req.getServletContext().getRealPath("/WEB-INF/fileName.properties"))));
System.out.println(props);

我发现最简单的方法是

  • 将文件放在src / main / resources / config.properties中。 当项目被maven编译成WAR时,它将被放在/WEB-INF/classes/config.properties中。

  • 使用以下命令从servlet读取文件

    InputStreaminputStream = getClass()。getClassLoader()。getResourceAsStream(“ config.properties”);

https://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM