Webapp file organization convention (development structure)
Solution 1
You can create the Java Web project is some popular IDE, like Eclipse, NetBeans, IntelliJ IDEA, to see the typical Java Web application structure.
And there is a difference between the development structure and packaging structure.
While developing an app you can pretty much use whatever structure you like. But you must package Java EE web application according to the specific rules.
See the official Java EE tutorials for the details:
- The Java EE 8 Tutorial: Packaging
- The Java EE 7 Tutorial: Packaging
- The Java EE 6 Tutorial: Packaging Applications
- The Java EE 6 Tutorial: Example Directory Structure (here just ignore info related to NetBeans)
Also here is the recommended conventions for structuring applications developed using Java 2 Platform, Enterprise Edition (although dated but might be useful):
Java Blueprints Guidelines.
Project Conventions for Enterprise Applications
And here an example from the above Java BluePrints:
UPDATE
Here is the example from one of my Java Web application project with Spring. For instance I stored all Spring related configuration files in the specially created spring folder inside WEB-INF. And in the spring folder I created more folders to better organize my app. Again, it is just one of the possible variants, i.e. a matter of personal preferences.
Solution 2
The web.xml needs to be in the WEB-INF directory. That's the only place app servers will look for it. Other than that, the spring xml files can be in the resources (which will end up in the classpath).
sp00m
Updated on October 14, 2020Comments
-
sp00m over 3 years
For the webapps I'm developing, I usually use the following files organization, since I think it respects the convention:
src |-- main |-- resources | |-- *.properties | |-- *.xml | |-- spring | |-- applicationContext.xml (main application context config file) |-- webapp |-- WEB-INF |-- spring | |-- spring-mvc.xml (web application context config file, delegated to manage only the web part) | |-- spring-security-http.xml (web security config) |-- static | |-- *.css | |-- *.js |-- views | |-- *.jsp |-- web.xml (deployment configuration)
What I would like to try, is to organize my files according to the following structure:
src |-- main |-- resources | |-- *.properties | |-- *.xml | |-- web.xml | |-- spring | |-- applicationContext.xml | |-- spring-mvc.xml | |-- spring-security-http.xml |-- webapp |-- WEB-INF |-- static | |-- *.css | |-- *.js |-- views |-- *.jsp
Of course, when packaging the webapp, the files will be relocated where they have to (e.g. the web.xml file within the WEB-INF folder). The reason why I would like to reorganize my webapps as above, is that I find it more convenient to have all the *.xml config files in the same location, instead of having some here and some there. Is it a bad idea in your opinion to break my initial structure? If yes why? Why is it so important to have all the web config files within the WEB-INF folder?
PS: technically, I know how to well link all the files within the classpath of the webapp. The question is more about convention and feedbacks from personal/professional experiences.
-
sp00m over 11 yearsThank you for your answer. I was talking about the development structure, I know that when packaging, the web.xml file should be within the WEB-INF folder. Could you provide the typical structure you're talking about? I found the first structure on springfuse.com.
-
sp00m over 11 yearsRight, thanks! And what about putting the web.xml in the src/main/resources during the development, and relocate it within the WEB-INF folder when packaging?
-
Andres Olarte over 11 yearsYou could certainly move it during packaging. However, it would be counter intuitive to someone looking at your application. Looking at your structure, it seems to be the standard Maven webapp structure. I would stick with keeping things in their natural location.
-
informatik01 over 11 yearsDeleted my first statement about your directory structures being unusual. It is indeed standard Maven structure (maven.apache.org/guides/introduction/…) as @AndresOlarte noted. Haven't thought about Maven structure, sorry. Just wrote about non-Mavenized directory structures ...
-
parsecer over 5 yearsSo you say that
WEB-INF
should be placed outside ofsrc
, intoWebContent
. However, apache guide suggests the opposite: maven.apache.org/plugins/maven-war-plugin/examples/… -
parsecer over 5 years@informatik01 See my comment above. In another maven guide OP's structure is considered correct.
-
informatik01 over 5 years@parsecer Em, actually that was not Maven project at all (Apache Ant was used for dependency management), and the general directory structure was generated by Eclipse for a web project. That is why there was "Web Content" directory etc. Plus the question is NOT Maven specific too, but is related to the requirements described in the Java EE specification. Also see the directory structure from the official Oracle Java EE Tutorial.
-
parsecer over 5 years@ informatik01 Oh, I see. Thanks for clearing it up.
-
informatik01 about 5 years[Correction for my comment above] Apache Ant was used as a build tool (Ant does not provide dependency management functionality like Maven or Gradle). And when I used Apache Ant back in the day, I used Apache Ivy for dependency management.