17

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
sp00m
  • 47,968
  • 31
  • 142
  • 252

2 Answers2

18

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:

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: Web Applications: Recommended Directory Structure


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.

Spring project structure example

informatik01
  • 16,038
  • 10
  • 74
  • 104
  • Thank 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 Jan 07 '13 at 17:17
  • Deleted my first statement about your directory structures being unusual. It is indeed standard Maven structure (http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html) as @AndresOlarte noted. Haven't thought about Maven structure, sorry. Just wrote about non-Mavenized directory structures ... – informatik01 Jan 07 '13 at 19:35
  • So you say that `WEB-INF` should be placed outside of `src`, into `WebContent`. However, apache guide suggests the opposite: http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html – parsecer Feb 14 '19 at 17:26
  • @informatik01 See my comment above. In another maven guide OP's structure is considered correct. – parsecer Feb 14 '19 at 17:27
  • @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](https://docs.oracle.com/javaee/6/tutorial/doc/gexap.html) from the official Oracle Java EE Tutorial. – informatik01 Feb 14 '19 at 19:15
  • @ informatik01 Oh, I see. Thanks for clearing it up. – parsecer Feb 14 '19 at 19:36
  • [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_](http://ant.apache.org/ivy/) for dependency management. – informatik01 Apr 12 '19 at 22:24
4

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).

Andres Olarte
  • 4,380
  • 3
  • 24
  • 45
  • Right, 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? – sp00m Jan 07 '13 at 17:19
  • 3
    You 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. – Andres Olarte Jan 07 '13 at 17:44