Webapp file organization convention (development structure)

随声附和 提交于 2019-11-26 19:38:25

问题


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.


回答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.




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



来源:https://stackoverflow.com/questions/14198942/webapp-file-organization-convention-development-structure

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