Spring MVC Project Structure



In this article, we will discuss the recommended way to structure your Spring MVC web application projects. Here we will also discuss the packaging structure and where to keep JSP files etc.

Spring MVC Project Structure

In a typical Spring MVC web application, the web application packaging structure may look:
── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── companyname
    │   │           └── projectname
    │   │               ├── domain
    │   │               │   └── MyDomain.java
    │   │               ├── repository
    │   │               │   └── MyDomainRepository.java
    │   │               ├── service
    │   │               │   ├── MyDomainService.java
    │   │               │   └── internal
    │   │               │       └── MyDomainServiceImpl.java
    │   │               └── web
    │   │                   └── MyDomainController.java
    │   ├── resources
    │   │   ├── META-INF
    │   │   │   └── spring
    │   │   │       ├── applicationContext.xml
    │   │   │       └── database.properties
    │   │   ├── logback-access.xml
    │   │   └── logback.xml
    │   └── webapp
    │       ├── WEB-INF
    │       │   ├── classes
    │       │   ├── i18n
    │       │   ├── layouts
    │       │   ├── spring
    │       │   │   └── webmvc-config.xml
    │       │   ├── views
    │       │   │   ├── myDomain
    │       │   │   │   ├── create.jsp
    │       │   │   │   ├── list.jsp
    │       │   │   │   ├── show.jsp
    │       │   │   │   └── update.jsp
    │       │   │   ├── dataAccessFailure.jsp
    │       │   │   ├── index.jsp
    │       │   │   ├── resourceNotFound.jsp
    │       │   │   ├── uncaughtException.jsp
    │       │   │   └── views.xml
    │       │   └── web.xml
    │       ├── images
    │       └── styles
    ├── site
    │   ├── apt
    │   ├── fml
    │   ├── site.xml
    │   └── xdoc
    └── test
        ├── java
        │   └── com
        │       └── companyname
        │           └── projectname
        │               └── service
        │                   └── MyDomainServiceTests.java
        └── resources
            ├── com
            │   └── companyname
            │       └── projectname
            │           └── service
            │               └── MyDomainServiceTests-context.xml
            └── logback-test.xml
Production
  • src/main/java – Java Source code packages and classes
  • src/main/resources – NON-Java Resources, such as property files and Spring configuration
  • src/main/webapp - deployment descriptor(web.xml), WEB-INF, JSP views under WEB-INF folder, static resources.
Test
  • src/test/java – Test Source code packages and classes
  • src/test/resources – NON-Java Resources, such as property files and Spring configuration

Spring MVC - placing your JSP files in a directory under the 'WEB-INF' directory

As a best practice, we strongly encourage placing your JSP files in a directory under the 'WEB-INF' directory so there can be no direct access by clients. 
Example:
    │   └── webapp
    │       ├── WEB-INF
    │       │   ├── classes
    │       │   ├── i18n
    │       │   ├── layouts
    │       │   ├── spring
    │       │   │   └── webmvc-config.xml
    │       │   ├── views
    │       │   │   ├── myDomain
    │       │   │   │   ├── create.jsp
    │       │   │   │   ├── list.jsp
    │       │   │   │   ├── show.jsp
    │       │   │   │   └── update.jsp
    │       │   │   ├── dataAccessFailure.jsp
    │       │   │   ├── index.jsp
    │       │   │   ├── resourceNotFound.jsp
    │       │   │   ├── uncaughtException.jsp
    │       │   │   └── views.xml
    │       │   └── web.xml
There can be different approach as per organization but above approach is typical Spring MVC project structure.

Spring MVC Project Structure Example

I have created a spring MVC Todo Management web application using Spring BootSpring MVCSpring SecurityJSPJPA and MySQL as a database. Here is standard project structure I have created:
To know how to create step-by-step SpringMVCc web application at Mini Todo Management Project using Spring Boot + Spring MVC + Spring Security + JSP + Hibernate + MySQL.

One more approach: you take a look at Spring's Project Sagan. It's the source code for their current website (http://spring.io). This site was written by the Spring team the way they would use their own tools and released as a reference application to answer questions just like this. I encourage you to take a look here: https://github.com/RameshMF/sagan.

Related Spring MVC Posts


  • Spring MVC 5 - Hello World Example - In this article, we will learn how to create a simple Hello World Spring MVC Application using Spring MVC 5 +, JSP, Maven build tool and Eclipse IDE
  • Spring MVC 5 - Sign Up Form Handling Example - In this article, we will learn how to create and submit a simple form (signup form) in Spring MVC application using Spring MVC 5+, Maven build tool, JSP and Eclipse IDE or STS.
  • Spring MVC JSP Form Tags Tutorial - In this tutorial, we're going discuss all Spring MVC form tags and we will use important spring MVC form tags such as form tag, text fields tag, select tag, check-box(s), radio box(s), password tag, button tag, errors tag etc.

Comments