Spring MVC – move over Struts? Day two of the Spring Workshop

11

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.

Spring MVC

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.

Some of the terms that were discussed that need some additional analysis: OGNL (Object Graph Navigation Language), JavaSpace , DWR – Direct Web Remoting (DWR is easy Ajax. XMLHttpRequest without any hassle. It makes it simple to call Java code directly from Javascript. It gets rid of almost all the boiler plate code between the web browser and your Java code.). Also: JOTM (Java Open Transaction Manager): JOTM (Java Open Transaction Manager) is an open source standalone transaction manager implementing the XA protocol and compliant with the JTA APIs.

What’s Next?

Next on the menu are AOP, Transaction Management, Message Sources, JMX and Management and Remoting. I look forward to tomorrow.

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

11 Comments

  1. Hi All,

    I am working on Spring Framework, This thread is much helpful to me. Very nice discussion.
    Thanks you
    Vamsi.

  2. hi, every body ! now i do not know how to use ajax with struts(with struts-html.tld,struts-bean.tld…)and when i write out.print(“…”); so the browser do not understand how and it can not translate to

    tag HTMl. How can i do ?

  3. mohammad,
    I personally like spring’s view support more than struts.

    If you have tried to deliever anything respectable
    using stuts, you would have found your self doing things in a funny way or trying
    some unnatural method to get it to work with struts.

    The problem being struts taglib works by html generation. Thus you are limited by
    the way struts generates the html code. especially if you want to generate html yourself,
    you find that you can’t, because you are using a struts tag that generates it for you and
    if you don’t use it, you have to write the binding yourself which is ugly or find some other
    way to do it with the struts tag and don’t generate code yourself.

    in spring, the concept is that spring provides taglib to bind your object without generating html for you.
    In spring you still write your own html tags, thus you still have complete flexibility
    (well, almost…just a few issues…too detailed to discuss here, but much more flexible than struts).

    For all the power of struts convinience tags, there’s currently nothing I can’t find in JSTL…

    spring’s spring:message tag is also quite neat and more powerful then struts. it allows you to display
    a property message or scoped variable or static text and you can do html escaping and Javascript escaping.
    real neat.

    I would say spring is heading the right direction, althought because its still new, some convinience methods
    are still lacking and some issues need to be ironed out.

  4. Hai in your Jasper Example is realy super.But i want the supreport
    using jasper Reports, please use the above example Master is Department Table
    child is the Emp Table , please give the clear detail step by step. thanking you
    Amar

  5. hi !!!

    im very interested to use spring mvc framework. but it seems its more poor in view.
    i cant find usefull tag library in its view (something like struts html tag libarary)

    and a question :

    can i use AJAX with spring mvc framework ?

  6. Still written ‘Professional Struts Development’ in your article !
    “Professional Java Development with the Spring Framework” has already been on my wish-list for a while. Looking forward to reading it.

  7. Hi,

    Spring does not ‘natively’ support chaining, at least not in the way Lucas indicated where different controllers build up a ModelAndView to render. There are possibilities to chain controllers by for example using the forward: or redirect: prefix to the View name. This results in either a forward being issued (resulting in an additional request) or a redirect.

    Using the former, data bound in the ModelAndView will be available in the request (as attributes not in the ModelAndView anymore) and you can start creating a new ModelAndView. When using redirects, the model attributes will be added to the request at request parameters (not attributes) and a new request will be issued by the client. This is less than perfect when you need full-fledged objects in the model in your second controller.

    I’m thinking about / working on a more elegant approach to controller chaining and I hope to provide something for 1.4 or possibly 1.4.

    rgds,
    Alef Arendsen

  8. Of course I meant Professional Spring Development. I have changed the article accordingly.

  9. Alef discussed the production process of the new Professional Struts Development book

    Wow, they really switched over to Struts ? ;-)

  10. Anjan Bacchu on

    Hi There,

    Good post. “Professional Struts Development” — you mean “Professional Spring Development” book ??!

    BR,
    ~A