Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Introduction to Spring MVC Framework

In this article we will talk about the spring framework and its importance in java web development.

Spring is the popular framework of application development for the enterprise edition of Java and is used by many developers in order to create the quality applications. This framework helps the developer to pay attention on the business logic in a proper manner and it is being used in a lot of real world applications. The framework of spring consists of many parts which provide different services. The Spring Web MVC is a framework that is based on the requests and therefore it is known as the request driven framework. This framework defines many different interfaces for different related work that is required to be handled by using the modern request based Spring MVC framework.


The enterprise level applications can be developed by the application developers with the use of POJO (Plain Old Java Object). In this document, the Model View Controller (MVC) framework using the framework of spring will be discussed and with the help of a sample application the work will be done in a web application. The MVC is a Hyper Text Transfer Protocol (HTTP) and servlet based framework which provides the help in the development of web applications in an excellent and an easy manner respectively. The interfaces are simple and clear and it becomes easy for the Spring MVC developers in order to write the desired applications and create the logic respectively.

The framework of spring separates the work done of model, view and controller in an efficient manner and provides such important concepts through which the web application can be developed in an easy manner compared with other frameworks. There is a Dispatcher-Servlet available in spring is a servlet which inherits from the base class of HTTP-Servlet and declared inside the web.xml configuration file. The request should be properly mapped in order to handle the request by Dispatcher-Servlet by the use of URL mapping in the configuration file. It is allowed in the Spring Web MVC framework to use any object and there is no need in order to implement the special interfaces and classes for this framework.

Spring has the ability to provide many controllers in order to be used as the base classes. There is a need to create a one or more than one controller in order to initiate the logic of business and successfully create an object of model and view. The view component can be a Java Server Page (JSP) and the output related work can be show in it respectively. The related configurations should be handled properly so that the work of an application is done as desired by the developer. There is an availability of annotations in spring framework in order to declare the related functionalities and the related logic to be used in the web application respectively.


The MVC is a way by which the logic, view and user input work gets separated and the work is done in a clean manner and can be verified easily. The V in MVC is the view which is basically the output of the work done in an application and this is separate from the logic of work and other related user interaction with the application regarding input. The M in MVC is the model which is basically the business logic work in an application and it is separate from the output and the related user interaction regarding input work. The C in MVC is the controller which deals with the work of user interaction regarding input with the application and it is separate from the business logic and output related work.

The interaction in MVC is done in the following manner; first the user uses the controller and from controller the related input goes to the model and then the model updates the view if there has been added any new related functionality and finally the correct desired output is viewed by the user and shown in the form of proper response. In order to manage the objects of business the spring framework provides a respective way.

An overview how the web application including the spring MVC framework, the work will be done in this manner, first the Dispatcher-Servlet receives the request as discussed about this servlet above, then this servlet asks the Handler-Mapping and initiate the controller that was asked with the request, after that this controller makes the progress on the request and calls the respective methods of service and respectively returns to the Dispatcher-Servlet an object of model and view that was created, this model and view object consists of the data of model and name of view, after this the Dispatcher-Servlet send the name of view in order to find out the exact view, after that the respective servlet passes the object of model to view in order to provide the result and finally the view by the help of the data of model will provide the actual results in the form of final output to the user.


This framework basically helps to make the development in Java EE easy and powerful in order to create performance based and reusable code for the applications that can be developed by the application developers respectively. The importance of MVC pattern is to separate the work of each part in an application and to make the code reusable respectively. The three components are not totally disconnected with each other but the work is done in this manner from which it looks like they are working on their own parts and completing the tasks respectively. The interaction is done within an application and the logic, output and input work is done in a separate manner to show the clear results from the working application and that is why this MVC pattern is used in the web development. The framework of spring is very good in order to test the code written by the developer.


The Spring MVC web application has been created and after the successful work of the configurations the screenshots are presented from the working application to show the related work.

Project Files and Output of Spring Web MVC Application

Figure 1: Project Files and Output of Spring Web MVC Application

This screen shows the output of an application and at the left the important libraries used in this web application to get the successful result.

Output of HTTP Server Monitor

Figure 2: Output of HTTP Server Monitor

This screen shows the HTTP server monitor when the application has been executed and in this screen the related details of used server and version details of java have been displayed.

Output of Request in HTTP Server Monitor

Figure 3: Output of Request in HTTP Server Monitor

This screen shows the output of request available in HTTP server monitor and the related attributes and the left side of projects tab show the libraries of spring that are used in order to execute this application in a successful manner.

This is the output of web application in the web browser

Figure 4: This is the output of web application in the web browser

The figure 4 shows the output of JSP and the related details present inside that page.

The message entered in the text-field

Figure 5: The message entered in the text-field

This screen shows the message entered by the user in the required text-field and the entered message as well.

Final required and expected Output

Figure 6: Final required and expected Output

This is the message and the final result when the work of application has completed in a successful manner.

Implementation & Codes:

Following are the codes and entered from the running application and the code is according to the given above screenshots.

Listing 1: index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome to Spring Web MVC project</title>

        <p>Hello! This is the default welcome page for a Spring Web MVC project.</p>
        <p><i>To display a different welcome page for this project, modify</i>
            <tt>index.jsp</tt> <i>, or create your own welcome page then change
                the redirection in</i> <tt>redirect.jsp</tt> <i>to point to the new
                welcome page and also update the welcome-file setting in</i>

This is a default JSP page of the web application and it executes are the start after the successful creation of web application.

Listing 2: theview.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <h1> ${This_is_an_actual_message}</h1>

This is the final view page of JSP and the output screen is shown above when all the related processing work of an application has completed successfully.

Listing 3: tnameView.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="" prefix="spring" %>
<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>The view related to this application and in JSP Page</title>
        <h1>Enter the message:- </h1>
        <p style="background-color: green">Enter the message and check the actual expected message after clicking the button respectively</p>
        <spring:nestedPath path="tname">
                           <form action="" method="post">
                               Please enter the message: 
                               <spring:bind path="tvalue">
                                            <input type="text" name="${status.expression}" value="${status.value}">
                                            <input type="submit" value="Enter">

This is the page in which the user enters the value and the related message then passed to the view page in which the entered message and expected output is displayed.

Listing 4

package com.mvc.control;
public class TName {
    private String tvalue;

    public String getTvalue() {
        return tvalue;
    public void setTvalue(String tvalue) {
        this.tvalue = tvalue;

This is the class that contains an attribute and the respective getter and setter is also generated. This attribute is used to get the message from user.

Listing 5:

package com.mvc.control;
import com.mvc.web.spring.framework.NewClass;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class Work_Controller extends SimpleFormController {
    private NewClass nc;

    public void setNc(NewClass nc) { = nc;
    public Work_Controller() {
        //Initialize controller properties here or 
        //in the Web Application Context

//    @Override
//    protected void doSubmitAction(Object command) throws Exception {
//        throw new UnsupportedOperationException("Not yet implemented");
//    }
    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
     protected ModelAndView onSubmit(
     HttpServletRequest request, 
     HttpServletResponse response, 
     Object command, 
     BindException errors) throws Exception {
         TName tname=(TName) command;
         ModelAndView mv = new ModelAndView(getSuccessView());
         mv.addObject("This_is_an_actual_message", nc.themess(tname.getTvalue()));
         return mv;

This Work_Controller class controls the work done in an application and many of the important methods and calls are generated through this class as it basically controls the application and passes the related information to view so that it can display in the form of an output to the viewer the related data respectively.

Listing 6:

package com.mvc.web.spring.framework;
public class NewClass {
    public static String themess(String nn){
        return "This is the message \" "+nn+" \" which you have entered";

When the work of control and model is done then at the end this is the expected output which is shown in the screenshot above. This work is called when the configurations are handled in a proper and the possible way.

Listing 7: applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean name="nc" class="com.mvc.web.spring.framework.NewClass"/>
    <!--bean id="propertyConfigurer"
          p:location="/WEB-INF/" />

<bean id="dataSource"
    p:password="${jdbc.password}" /-->

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->


This is the configuration file and the configuration of class named as NewClass has been handled in it respectively in order to call the related work at the right time.

Listing 8: dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean class=""/>

    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
                <prop key="index.htm">indexController</prop>

    <bean class="com.mvc.control.Work_Controller" p:nc-ref="nc"/>

    <bean id="viewResolver"
          p:suffix=".jsp" />

    The index controller.
    <bean name="indexController"
          p:viewName="index" />

This configuration file contains the main logic, the related information of controller available in it and also some important tags used to complete the work of application using the spring libraries.

Listing 9: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

This is the main configuration file and the calls at the initialization time are made from this file. Therefore, all the configurations from this file should be handled with care because ay small change can put an effect on the functionality of an application.

This is all for this article. Hope you liked it.

My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login