SpringMVC Classroom image

Anil  Bist / Professional / Web Technology

To post your Question Join Classroom
Lesson Topics's No:-  ||
Lessons:-Spring Web MVC Concepts

Spring Web MVC Concepts

The world of Java has seen many MVC design pattern-based web frameworks crop up in the past few years (several are listed at the very end of this chapter). MVC was originally conceived at XEROX PARC around the 1978–79 time frame and was later implemented in the Smalltalk-80 class library (also at XEROX PARC). It is a relatively simple concept to grasp and provides for a clean separation of presentation and data, as I’ll explain briefly here.

First, let’s look at our architecture diagram established earlier in the book and shown here in Figure 7.1.

As you can see, all incoming HTTP requests from a web browser are handled by Controllers. A controller, as the name indicates, controls the view and model by facilitating data exchange between them.The key benefit of this approach is that the model can worry only about the data and has no knowledge of the view.The view, on the other hand, has no knowledge of the model and business logic and simply renders the data passed to it (as a web page, in our case).The MVC pattern also allows us to change the view without having to change the model.

     Let’s review some basic Spring MVC concepts. First, we will look at the concepts related to Java coding, and then we will look at the configuration required to make all this work.

Spring MVC Java Concepts

Figure 7.1 provided us a high-level view of the architecture for Time Expression. Now let’s take a slightly more detailed and focused look at the Spring MVC components. Figure 7.2 shows an end-to-end flow for a typical screen in Time Expression.This diagram shows many of the concepts we will discuss next.


Spring provides many types of controllers.This can be both good and bad.The good thing is that you have a variety of controllers to choose from, but that also happens to be the bad part because it can be a bit confusing at first about which one to use. The best way to decide which controller type to use probably is by knowing what type of functionality you need. For example, do your screens contain a form? Do you need wizardlike functionality? Do you just want to redirect to a JSP page and have no controller at all? These are the types of questions you will need to ask yourself to help you narrow down the choices.

Figure 7.3 shows a class diagram of some of the more interesting controllers that are part of Spring MVC.Table 7.1 provides brief descriptions on the interface and classes shown in Figure 7.3. (Note:The descriptions provided in this table are taken directly out of the Spring Framework Javadocs.) I tend to use SimpleFormController, UrlFilenameViewController, and AbstractController most often.We will see examples of these later in this chapter.

Description of Various Spring Controllers

Controller                                     Description (Taken Directly from the                                                           Spring Javadocs)

AbstractCommandController :  Abstract base class for custom command                                                         controllers.

AbstractController  :                   Convenient superclass for controller                                                                 implementations, using the Template                                                                Method design  pattern.


Controller                                      Description (Taken Directly from the                                                             Spring Javadocs)

AbstractFormController  :           Form controller that autopopulates a form                                                       bean from the request.

AbstractUrlViewController  :       Abstract base class for Controllers that                                                            return a view name based on the URL.

AbstractWizardFormController :  Form controller for typical wizard-style                                                              workflows.

BaseCommandController :           Controller implementation that creates                                                              an object (the command object) on                                                                  receipt of a request and attempts to                                                                populate this object with request                                                                      parameters.

CancellableFormController :         Extension of SimpleFormController that                                                          supports “cancellation” of form                                                                          processing.

Controller                             :         Base Controller interface, representing a                                                          component that receives                                                                                  HttpServletRequest and                                                                                    HttpServletResponse like a HttpServlet                                                            but is able to participate in an MVC

ParameterizableViewController :  Trivial controller that always returns a                                                               named view.

SimpleFormController :                 Concrete FormController                                                                                   implementation that provides                                                                           configurable form and success views,                                                             and an onSubmit chain for convenient                                                             overriding.

UrlFilenameViewController :          Controller that transforms the virtual                                                                 filename at the end of a URL into a                                                                 view name and returns that view.

Model and View

Many of the methods in the Controller related subclasses return a org.
springframework.web.servlet.ModelAndView object.This object holds the model (as a java.util.Map object) and view name and makes it possible to return both in one return value from a method.We will see examples of this later in this chapter when we build two of the screens for Time Expression.

Command (Form Backing) Object

Spring uses the notion of a command object, which essentially is a JavaBean style class that gets populated with the data from an HTML form’s fields.This same object is also passed to our validators (discussed next) for data validation, and if the validations pass, it is passed to the onSubmit method (in controller related classes) for processing of valid data. Given that this command object is a simple JavaBean-style class, we can use our
business objects directly for data binding instead of writing special classes just for data binding. I will demonstrate this benefit later in this chapter.


A Spring validator is an optional class that can be invoked for validating form data for a given command (form) controller.This validator class is a concrete class that implements the org.springframework.validation.Validator interface. One of the two methods required by this interface is the validate method, which is passed a command object, asmentioned previously, and an Errors object, which can be used to return errors. I will demonstrate an example of a Validator class later in this chapter. Another notable validation
class is org.springframework.validation.ValidationUtils, which provides
convenient methods for rejecting empty fields.

Spring Tag Library (spring:bind)

The spring bind tag library is simple yet powerful. It is typically used in JSP files via the <spring:bind> tag, which essentially binds HTML form fields to the command object. Furthermore, it provides access to special variables within JSP, such as ${status.value}, ${status.expression}, and ${status.errorMessages}, which we will look at later in the chapter.

Anil  Bist

Skills    SpringMVC

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a


  Students (0)