Spring MVC – move over Struts? Day two of the Spring Workshop
Today I attended the second day of the Spring Workshop by Rod Johnson and Alef Arendsen, two of the core team of the Spring Framework. Having been thoroughly lectured on Inversion of Control and a thousand different ways for configuring beans in the Spring IoC Container on the first day, today we were introduced into the wonderful world of the Spring MVC Framework. Spring MVC is a modern competitor for Struts. Based on concepts such as IoC, programming against interfaces, abstracting View-implementations from the Controllers, clearly separating the Model from the View – and having the Controller access the Model (Business Service) through Dependency Injection and therefore without dependencies on concrete class implementations – are some of the main concepots of Spring MVC.
Unit Testing with EasyMock
The first topic of the day was the use of EasyMock (EasyMock provides Mock Objects for interfaces in JUnit tests by generating them on the fly using Java’s proxy mechanism. Due to EasyMock’s unique style of recording expectations, most refactorings will not affect the Mock Objects. So EasyMock is a perfect fit for Test-Driven Development.). Alef demonstrated how we can easily implement unit tests on classes with dependencies on other objects that have not yet been implemented! We do not need to create Mock Objects for such classes, we can have EasyMock generate such classes dynamically, using our configuration details to specify what values the methods in the MockObjects need to return. This allows for example for out-of-container testing for objects that depend on HttpRequest or a DataSource.
Next was Spring MVC. Alef introduced the history of Web Application architectures, from CGI to Model 2 and Event Driven. He gave a brief overview of two of the most prominent MVC frameworks around: Struts and WebWorks2. He stipulated a number of issues with each of these, such as design decisions that may have been good at the time but would now be made differently.
Alef clearly stated that Spring MVC chooses to incorporate Domain Objects in the Web-Tier instead of abstracting them away, such as Struts does with ActionForms. Spring MVC will include Domain Classes in the Model (Map) that is presented to the View for use in the render-process. Spring has facilities for binding the HttpRequest (Attributes) onto the Domain Class (the FormBackingObject) that is associated with a Controller. This bind-step can make use of advanced property editors for converting String values from the Request to the typed properties on the Domain Class; you can also register custom property editors, for example for dates and currency values. The View – except JSP of course – is not directly linked to the HttpRequest object; the Controller is. The Controller prepares a Map for the View to use as Model to retrieve data from.
Spring MVC has support for many types of View implementations, including JSP, XSLT, PDF and Excel. There is special support for JSTL in JSP – and it should be clear that JSTL en EL are the way for JSPs to extract values from the Model. Spring has Abstract classes for PDF and Excel that do some initialization and set up; to implement a View component, extending the AbstractView class is all you need to do.
Alef briefly discussed WebFlow, soon to be released as subproject under Spring. WebFlow is to be used for defining complex process flows such as multi-page wizards and page-spanning web based processes. WebFlow allows for declarative specification of the flow, including conditional transitions and multiple process steps.
We also went into Controller Chaining (akin to Action Chaining in Struts) where several finer-grained Controllers each perform a smaller task that together build up to a View. Such chaining makes it possible to reuse Controllers. Alef did not show a detailed example of Controller chaining but suggested a couple of approaches.
Today we had more hands-on labs than yesterday, which is a good thing. Programming with Spring was fun, catering for the initial set up and configuration detail is less fun. We used Eclipse 3.0.2, JDK 1.4.2, Tomcat 5.0.28 and a Progress database for the labs. We built a small JSP based Web MVC application, implementing a Controller that extracts data from the Request and builds a Model using an injected Business Service, finally returning a View – a logical indication that is turned into a JSP through the configuration details in the application context definition file. Later on we also configured a HandlerInterceptor that intervenes just prior to the invocation of the Controller object.
During the lunch break Alef discussed the production process of the new Professional Java Development with Spring book, due to be released on 20th June, just in time for JavaOne. It has been hard work – co-authoring with most of the core committers such as Jurgen, Rod, Rob. He admitted to also have written a preface in which he apologizes to his family and thanks them for their support. It really is hard work, writing a book.
Next on the menu are AOP, Transaction Management, Message Sources, JMX and Management and Remoting. I look forward to tomorrow.