Reading Web Application Resources

前端 未结 3 1250
别跟我提以往
别跟我提以往 2020-12-19 09:45

Background

Developing a simple web application (Eclipse + JBoss + Apache Tomcat) to generate XML files.

Problem

The \"Business Area\" list querie

相关标签:
3条回答
  • 2020-12-19 09:53

    The right location (and also the common practice) is to place them under your source directory, which will then gets compiled into WEB-INF/classes directory. I'm not sure what you meant by "classes directory is volatile" in your response to @Dave, but this is how most (if not all) Java web apps store things. WEB-INF/classes is not just for Java classes. It's common to see logging properties file (like log4j), Hibernate and Spring XML files stored under source directory and you can safely access the files using something like this:-

    // in this case, the business-areas.sql is located right under "source/sql" directory
    InputStream is = getClass().getClassLoader().getResourceAsStream("sql/business-areas.sql");
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    

    Some useful information about the use of META-INF: What's the purpose of META-INF?

    0 讨论(0)
  • 2020-12-19 09:57

    I'd put them in WEB-INF/classes, or bundle them inside your application.jar which will go inside WEB-INF/lib. Then you can load them from the classpath as explained here and here

    Even better, if you use maven, the best practice is to put these type of files inside src/main/resources and then maven will take care of this for you.

    0 讨论(0)
  • 2020-12-19 10:16

    I had similar concerns as Dave Jarvis about mixing resources with classes and lib, so I did some fiddling and found this solution:

    I placed my resource files in WEB-INF/resources. Then, to load them, I used this:

    getClass().getClassLoader().getResourceAsStream("../resources/main.xml");
    

    I don't know that using a .. is a much cleaner solution, but my files are at least not mixed with classes or jars.

    0 讨论(0)
提交回复
热议问题