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

别等时光非礼了梦想. 提交于 2019-12-24 17:48:50

问题


It seems that in my Tapestry app, I can't load ini files nor properties file from WEB-INF directory or class path.

I tried several different methods which should load my file but non of them worked.

  1. ex

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

  1. ex

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

I need to load properties/ini file in order to use tapestry-security module which is based on Shiro.

Thanks for help !


回答1:


The root of the classpath is the way to go. Put your file in src/main/resources/auth.properties then set your resourcePath using realm.setResourcePath("classpath:auth.properties");

Check the ExtendedPropertiesRealm and the tapestry-security testapp for an example

  • http://svn.codehaus.org/tynamo/trunk/tapestry-security/src/test/java/org/tynamo/security/testapp/services/AppModule.java

  • http://svn.codehaus.org/tynamo/trunk/tapestry-security/src/test/resources/shiro-users.properties




回答2:


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

ServletContext has to be use from servlet, servletListener etc.




回答3:


Try

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



回答4:


I found the easiest way was to

  • put file in src/main/resources/config.properties. This will be put in /WEB-INF/classes/config.properties when the project is compiled by maven into WAR

  • read the file from a servlet with the following

    InputStreaminputStream = getClass().getClassLoader().getResourceAsStream("config.properties");

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



来源:https://stackoverflow.com/questions/9663564/how-to-load-files-properties-from-web-inf-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!