Getting Started with Spring 4 MVC with Maven and XML Configuration

Getting Started with Spring 4 MVC with Maven and XML Configuration
February 2, 2017 No Comments Uncategorized Vikram Patil

Spring 4 MVC is the latest version of Java MVC framework. This is a improved version of Spring 3 MVC. In this blog we will discuss how to create a Simple web application in Spring 4 MVC with Maven and XML Configuration in Eclipse.
Technologies used:
1. Spring 4.3.0.RELEASE
2. JDK 1.7
3. Maven 3
4. Apache Tomcat 7.0
5. Eclipse Java EE IDE

1. Create a new Maven project

First go to File->New->Project. New Project dialog box will be display then select Maven->Maven Project and click Next button.

New Project

From New Maven Project dialog box, Check Use default Workspace location otherwise uncheck it and browse different Workspace location then click on Next button.

New Maven Project

Then select maven-archetype-webapp from project Archetype and click on next button.

archtype

Provide the Group Id and Artifact Id for the project and click on finish button.group_id

Note: After creating the project if you facing below shown error then right click on project and select Build Path->Configure Build Path..->Targeted Runtimes -> check Apache Tomcat v7.0 and click on Apply then Ok button.

error

2. Adding Spring 4 libraries as dependencies in pom.xml

Now add spring 4 libraries as dependencies in pom.xml file which is required for our application. Update pom.xml file as shown below:

xml1

3. Configure dispatcher-servlet.xml
This file place inside the WEB-INF folder. You can give any name to this file.
This xml file holds the configuration information for the view resolver that spring uses to map view names to a concrete view files.

dispatcher

In above file tag allow spring to load all components from package com.spring,controller and all its child packages.
This next bean will resolve the view and add prefix string /WEB_INF/views/ and suffix .jsp to the view in ModelAndView.

4. Configure web.xml

webxml

This is the typical configuration for a Spring MVC-based application with declaration for Spring’s DispatcherServlet and ContextLoaderListener along with the Spring configuration files dispatcher-servlet.xml. It specifies the URL mapping for Spring’s DispatcherServlet to handle all requests.

5. Create Controller:
Create HelloController under the src/main/java directory.
We are create com.spring.Controller package in src/main/java directory and add this controller.

controller

In above code @Controller annotation is used to specify this class is a Spring controller.
@RequestMapping annotation maps web requests onto specific handler classes and/or handler methods. In above code @RequestMapping annotation handle welcomeMessage method.

@RequestParam annotation indicates that a method parameter should be bound to a web request parameter. In above code @RequestParam bound web request parameter name with method parameter name.

ModelAndView define the target view in Spring MVC. In above code addObject method set the attributes message and name and satViewname method define the target view welcome.jsp.

6. Create View:
index.jsp:
This default jsp file edit like below.

index

The href send the web request to controller through url with name web parameter.

welcome.jsp:
This file create under /WEB-INF/views directory.

welcome

In above code ${message} and ${name} access the values which is set using addObject method of ModeAndView in Controller.

7. Run on Server
Right click the project and select Run As –> Run on Server.

output1

Click on Click Here link then below output will be display.

output2

About The Author

Leave a reply

Your email address will not be published. Required fields are marked *