Posted on by & filed under Content - Highlights and Reviews, Web Development.

Spring MVC is an implementation of the Model-View-Controller pattern for the Web. To ensure smooth transitions between the view, the controller, and the underlying application, it is useful to have a well-defined data model with clear separations of concern at each layer. This tip will walk through modelling data in a typical user-interactive Spring MVC application.

The source code for this tip is available at:


Data flow through the layers of an MVC application can get tricky when it comes to mapping the logical models to the data itself. It is tempting to pick a single, generic representation of data to use from layer to layer, but this approach forces a fundamental data model impedance upon every layer of the application.

The three most common layers of an MVC Web application are the view, controller, and persistence layers. The view layer generally corresponds to form-based data filled out by the user, and hierarchical data presented to the user. The controller layer might pull information in from the side, in a hybrid hierarchical/relational style. The persistence layer, depending upon the backend implementation, might be entirely hierarchical, relational, or some balance of the two.

Take a look at Chapter 3 in the Pro Spring MVC: With Web Flow title for more details about Spring’s MVC Pattern.

Separation of concern

Spring MVC’s <form /> JSP element binds best to flat, JavaBean-style instances with fields and “getter/setter” methods.

These view-tailored models don’t always map well to the application domain model. The author data from the above form might be stored in a single Author class, while the message might be stored in an UnsavedComment class. This mapping can be done within the controller, which can pass the data along to the application logic in a more object-oriented structure.

If this data were bound for a relational database, it might need to be transformed yet again to fit with the table structure on the backend. This is especially true for more complex data models than the one used for this simple example.

Single responsibility principle

Separating the representation of data into discrete stages helps keep the design of an application more modular: form data is stored in a form-specific data structure, application data is stored in an application-specific data structure, and database-specific data is stored in a database-specific data structure.

There is some overhead imposed by translation between data models across application layers, but the benefit is significant: logically distinct sections of the application can work with data based upon the most appropriate model, and they don’t need to accommodate changes that might be made in logically separate areas of the application.

Safari Books Online has the content you need

Below are some Spring books to help you develop applications, or you can check out all of the Spring books and training videos available from Safari Books Online. You can browse the content in preview mode or you can gain access to more information with a free trial or subscription to Safari Books Online.

Pro Spring MVC: With Web Flow provides in-depth coverage of Spring MVC and Spring Web Flow, two highly customizable and powerful web frameworks brought to you by the developers and community of the Spring Framework.
Totally revised for Spring 3.0, Spring in Action, Third Edition is a hands-on guide to the Spring Framework. It covers the latest features, tools, and practices including Spring MVC, REST, Security, Web Flow, and more.
The release of Spring Framework 3 has ushered in many improvements and new features. Spring Recipes: A Problem-Solution Approach, Second Edition continues upon the bestselling success of the previous edition but focuses on the latest Spring 3 features for building enterprise Java applications.
Spring Batch in Action is an in-depth guide to writing batch applications using Spring Batch. Written for developers who have basic knowledge of Java and the Spring lightweight container, the book provides both a best-practices approach to writing batch jobs and comprehensive coverage of the Spring Batch framework.
Spring Web Services 2 Cookbook includes a wide variety of recipes that covers most important topics used in real-world applications. It is a well-rounded guide covering a lot of ground in the Spring Web Services domain using systematic arranged chapters and focused recipes.

Start your FREE 10-day trial to Safari Books Online

About this author

James Earl Douglas is a software developer focusing on functional Web APIs on the JVM. He has based many Java systems on Spring MVC and its ecosystem, which lead to authoring the e-book Barebones Spring MVC. James now researches similar and dissimilar approaches in Scala and Clojure.

Tags: JavaBean, modelling data, pattern, Spring MVC,

Comments are closed.