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

时间:2012-03-12 08:09:51

标签: java tapestry shiro tynamo

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

我尝试了几种不同的方法,这些方法应该加载我的文件,但没有它们工作。

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

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

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

      感谢您的帮助!

4 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:2)

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

检查 ExtendedPropertiesRealm 和tapestry-security testapp以获取示例

答案 2 :(得分:0)

试试

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

答案 3 :(得分:0)

我发现最简单的方法是

  • 将文件放在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/

相关问题