So far, we have seen how we can install all the prerequisite tools and software. Now we are going to develop our first Spring MVC application using STS. STS provides an easy-to-use project template. Using these templates, we can quickly create our project directory structures without much problem.
Let's create our first Spring MVC project in STS:
com.packt
and Artifact Id as webstore
. Then, select Packaging as war
and click on the Finish button, as shown in the following screenshot:We have successfully created a basic project, but we need to perform one small configuration in our pom.xml
file, that is, telling Maven to use Java Version 8 while compiling and building our project. How do we tell Maven to do this? Simply add two property entries in pom.xml
. Let's do the following:
pom.xml
; you can find pom.xml
under the root directory of the project itself.pom.xml
file. Select the Overview tab.maven.compiler.source
and Value as 1.8
, as shown in the following screenshot:maven.compiler.target
and Value as 1.8
.pom.xml
.We just created the basic project structure. Any Java project follows a certain directory structure to organize its source code and resources. Instead of manually creating the whole directory hierarchy by ourselves, we just handed over that job to STS. By collecting some basic information about our project, such as Group Id, Artifact Id, and the Packaging style, from us, it is clear that STS is smart enough to create the whole project directory structure with the help of the Maven. Actually, what is happening behind the screen is that STS is internally using Maven to create the project structure.
We want our project to be deployable in any servlet container-based web server, such as Tomcat or Jetty, and that's why we selected the Packaging style as war
. Finally, you will see the project structure in Package Explorer, as shown in the following screenshot:
The project structure of the application
If you encounter a maven error on your pom file saying web.xml is missing and <failOnMissingWebXml> is set to true, then it means it is expecting a web.xml
file in your Maven project because it is a web application, as we have chosen packaging as war
. However, nowadays in web applications web.xml
file is optional. Add the following configuration in your pom.xml
within <project>
tag to fix the error:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>