【问题标题】:How to load files/properties from WEB-INF directory?如何从 WEB-INF 目录加载文件/属性?
【发布时间】:2018-03-08 01:57:31
【问题描述】:

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

我尝试了几种不同的方法来加载我的文件,但都没有奏效。

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

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

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

感谢您的帮助!

【问题讨论】:

    标签: java tapestry shiro tynamo


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

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

    查看 ExtendedPropertiesRealm 和 Tapestry-security testapp 以获取示例

    【讨论】:

    • 好伙伴,谢谢它的魅力^^我仍然不知道为什么它在WEB-INF中不起作用:(
    【解决方案3】:

    试试

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

    【讨论】:

      【解决方案4】:

      我发现最简单的方法是

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

      【讨论】:

        猜你喜欢
        • 2018-01-27
        • 2015-06-29
        • 2011-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        • 2018-03-06
        相关资源
        最近更新 更多