Book Image

Spring MVC: Beginner's Guide - Second Edition

By : Amuthan Ganeshan
Book Image

Spring MVC: Beginner's Guide - Second Edition

By: Amuthan Ganeshan

Overview of this book

Spring MVC helps you build flexible and loosely coupled web applications. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly configurable. Also, Spring can integrate effortlessly with other popular web frameworks such as Struts, WebWork, Java Server Faces, and Tapestry. The book progressively teaches you to configure the Spring development environment, architecture, controllers, libraries, and more before moving on to developing a full web application. It begins with an introduction to the Spring development environment and architecture so you're familiar with the know-hows. From here, we move on to controllers, views, validations, Spring Tag libraries, and more. Finally, we integrate it all together to develop a web application. You'll also get to grips with testing applications for reliability.
Table of Contents (20 chapters)
Spring MVC Beginner's Guide - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Creating our first Spring MVC project


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.

Time for action - creating a Spring MVC project in STS

Let's create our first Spring MVC project in STS:

  1. In STS, navigate to File | New | Project; a New Project wizard window will appear.

  2. Select Maven Project from the list and click on the Next button, as shown in the following screenshot:

    Maven project's template selection

  3. Now, a New Maven Project dialog window will appear; just select the checkbox that has the Create a simple project (skip archetype selection) caption and click on the Next button.

  4. The wizard will ask you to specify artifact-related information for your project; just enter Group Id as com.packt and Artifact Id as webstore. Then, select Packaging as war and click on the Finish button, as shown in the following screenshot:

    Specifying artifact-related information during the project creation

Time for action - adding Java version properties in pom.xml

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:

  1. Open pom.xml; you can find pom.xml under the root directory of the project itself.

  2. You will see some tabs at the bottom of the pom.xml file. Select the Overview tab.

    Tip

    If you do not see these tabs, then right-click on pom.xml, select the Open With... option from the context menu, and choose Maven POM editor.

  3. Expand the Properties accordingly and click on the Create button.

  4. Now, an Add property window will appear; enter Name as maven.compiler.source and Value as 1.8, as shown in the following screenshot:

    Adding the Java compiler version properties to POM

  5. Similarly, create one more property with Name as maven.compiler.target and Value as 1.8.

  6. Finally, save pom.xml.

What just happened?

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

Tip

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>