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 Boot, Spring MVC, Spring Security, JSP, JPA 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.
- Spring MVC 5 Form Validation with Annotations Tutorial - In this quick tutorial, we're going to learn about Spring MVC form validation using annotations like @NotNull, @Size, @Min, @Max, @Email, @Pattern etc.
- Spring MVC 5 + Hibernate 5 + JSP + MySQL CRUD Tutorial - In this spring hibernate integration tutorial, we will learn how to create Spring MVC 5 web application, handle form submission, integrate hibernate 5 to connect to the backend database. In this tutorial, we will integrate Spring MVC 5+ with Hibernate ORM framework using Java-based configuration without any XML configuration.
- Spring MVC 5 + Spring Data JPA + Hibernate 5 + JSP + MySQL Tutorial - In this tutorial, we will discuss the integration of Spring MVC 5, Spring Data JPA, Hibernate 5 and MySQL CRUD example. We will demonstrate CRUD(Create, Retrieve, Update, Delete) operations on a Customer entity as well as display list of customers from the MySQL database.
- Spring MVC + Spring Boot2 + JSP + JPA + Hibernate 5 + MySQL Example - In this article, we will learn how to develop a Spring MVC web application using Spring MVC, Spring boot 2, JSP, Hibernate 5, JPA, Maven, and MySQL database.
- Spring Boot 2 MVC Web Application Thymeleaf JPA MySQL Example - In this article, we will learn how to develop a Spring MVC web application using Spring boot 2, Thymeleaf, Hibernate 5, JPA, Maven, and MySQL database.
- Spring MVC 5 + Hibernate 5 XML Based Configuration Example - In this tutorial, we will integrate Spring MVC with Hibernate ORM framework using XML-based configuration.
Comments
Post a Comment
Leave Comment