Web MVC with Spring and Business Objects

In the previous posts, we explored the MVC Pattern Language by Trygve Reenskaug and made an attempt to implement MVC in the console, while focusing on MVC’s overarching idea – supporting users’ mental models. In this post, we’ll take a quick look on how to move the (poor) console example to the web.

A Short Reminder

For those of you who haven’t read the previous post or don’t (want to) remember, we were creating a simple “Pet Clinic” application. We visited an old vet lady who does not own a computer to gather requirements and get a basic idea of what her work is about. For the most part, she’s using two interesting items at work: a visit calendar and pet files. These are perfect candidates to become business objects in our system. Since the lady is so used to them in the physical world, we assume she’ll have no problems using their computerized versions. So far, we created a console representation of the visit calendar. Now, we want to move it to the web so that it’s usable for non-nerd human beings.


In the console example, the view was a regular Java object, which talked to the VisitCalendar model object and displayed it on the screen:

In our Spring MVC version of the view, we want to replace the console printing stuff with HTML. Now, we have two choices:

  • HTML template is the view and talks to the model object directly
  • the view is a Java object responsible for filling the HTML template and rendering it (effectively i.e. it could simply return Spring’s ModelAndView)

In my code, I started with the first option but one could easily find reasons to go with the second. The interesting part of my view template looks like this (please, don’t learn HTML from me and don’t judge me, I know I suck at this):

As you can see (actually, I hope you just skimmed it and didn’t bother to understand), we’re showing our vet lady a page from her calendar i.e. one week of visits. She can insert visits into the calendar the same way she does with her physical one – if there is no visit on given time, you can “type in” a visit directly there. Overall, it renders something like this (shame rises):


Since the HTML template talks to the model directly, it had to change a little to accommodate template’s idiosyncrasies i.e. public methods and retrieving visits one-by-one. Regarding using it with Spring MVC, I had to make it a session scoped bean so that users don’t mess with each others calendar i.e. the currentWeek field:

When used with a real database, the visitOn method might quickly become too slow for the application to work smoothly. In such case, we could, for example, keep current week’s visits in model‘s or view‘s memory (the latter calls for using an object as a view instead of an HTML template).


The Spring MVC controller obviously looks very different from the console one, but the general idea and functionality remained:

This solution keeps the “next” and “previous” commands I used in the console solution. One could as well replace the /next and /previous endpoints with some sort of paging. The user experience would remain just as good, but we could avoid redirecting to the root path all the time and give users the possibility to share URLs to certain weeks.


Once you recognize the business objects in your system and their functionality, it’s really easy to move those into a framework like Spring MVC. The model that you implement is not very different from the one you’d implement for any other kind of application. The controller and the view will depend a lot on the underlying technology that you’re using. If it’s Spring MVC, implementing the former is pretty straightforward, while there are at least two good options for the latter. Now, the real idea behind this post is the same as in the previous one – the model, view, controller triplet is supposed to represent business objects inspired by the users’ mental models, instead of being a generic presentation framework.

About the Author Grzegorz Ziemoński

King of Tidy Java, nerd that thinks about producing perfect software all the time and proud owner of 2 cats.

follow me on: