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 197879 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.

Figure 7.1. High-level architecture diagram for Time Expression.


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.

Figure 7.2. End-to-end flow for Enter Hours screen using Spring and Hibernate.


Controller

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.

Figure 7.3. Class diagram showing a partial list of Spring controllers.


Table 7.1. 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.

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 workflow.

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.

Validator

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, as mentioned 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.

Spring MVC Configuration Concepts

In this section, we will review some core concepts related to configuring the Spring Web MVC Framework.

DispatcherServlet

DispatcherServlet (part of the org.springframework.web.servlet package) is the entry point to the world of Spring Web MVC, as depicted in Figure 7.2. It essentially dispatches requests to the controllers. If you have worked with Java web applications before, you will not be surprised to find out that this class is configured in the web.xml file, as shown in the following excerpt from the complete web.xml for Time Expression:

<servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>


We will discuss DispatcherServlet in detail later in this chapter.

Handler Mappings

You can map handlers for incoming HTTP requests in the Spring application context file. These handlers are typically controllers that are mapped to partial or complete URLs of incoming requests. The handler mappings can also contain optional interceptors, which are invoked before and after the handler. This is a powerful concept. I will demonstrate an example of this later in this chapter when we use such a web interceptor for authentication and close our Hibernate session for the given HTTP request.

The following code excerpt taken from our complete timex-servlet.xml file shows how a handler can be mapped to a partial URL:

<bean      >     <property name="urlMap">         <props>             <prop key="/signin.htm">signInController</prop>             <prop key="/signout.htm">signOutController</prop>         </props>     </property> </bean>


View Resolvers

Spring uses the notion of view resolvers, which resolve view names to the actual views (enterhours to enterhours.jsp, for example). We will use Spring's InternalResourceViewResolver class to resolve our view names. (This is covered in the next section.)



Agile Java Development with Spring, Hibernate and Eclipse
Agile Java Development with Spring, Hibernate and Eclipse
ISBN: 0672328968
EAN: 2147483647
Year: 2006
Pages: 219

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net