簡體   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