Spring 1.2 and Struts 1.2

What is Springframework?
Springframework is a loosely coupled component based framework where all, or none of the components can be utilized. Simply put, it is an integration tool. Springframework allows a developer to choose and use different packages of preference. For example, Spring allows you to specify Toplink with Struts, or Hibernate with Spring-MVC. This is the ability of Spring. Libraries can be seamlessly interchanged. If a software library is not built into the core package of Spring, then it is usually very simple to integrate the library into Spring’s IoC container. Packages from DWR to Sitemesh and others were easily incorporated into a production level Spring based application.

Struts is not the same
Struts is not the same animal as Spring. Struts can not really be compared directly to Spring. Struts is an action based framework with a plugin integration method. Struts' plugin design is usable, but not as architecturally sound as Spring's integration. Using Struts, there is no out of the box support for other tools than the action controllers. Spring has a lot of popular software libraries bundled into Spring's core allowing for instant utilization. So it is not necessarily far to compare Spring and Struts. Instead, it is only fair to compare Spring-MVC and Struts. Spring MVC is the Struts counterpart.

Code Comparison
All of the Controller coding for both Spring and Struts is defined through and XML file. There is not much difference as you can see below. Spring uses a controller bean definition while Struts defines the action bean.

Struts 1.2 configuration

    <form-bean  name="logonForm"  type="org.apache.struts.validator.DynaValidatorForm">
      <form-property  name="username" type="java.lang.String"/>
      <form-property  name="password" type="java.lang.String"/>
    <!--other form beans defined-->
  <!-- And later the action mapping -->
    <action path="/logon" name="logonForm" scope="request" type="com.test.actions.logon" 
        validate="true" input="layout.view_only" parameter="method">
      <forward name="update"        path="forward.reset.password.success.page" />
      <forward name="success"       path="forward.main.menu.success.page"/>
      <forward name="failure"       path="layout.view_only" />
    <!-- other actions defined -->

And for Spring-MVC the following mapping and definition is required to activate controller.

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
      <prop key="/rptGeneralInfo.htm">reportsGeneralInfoForm</prop>
      <!-- Other mappings -->
<bean id="reportsGeneralInfoForm" class="com.test.forms.ReportsGeneralInfoForm">
  <property name="formView"><value>reportsGeneralInfo</value></property>
  <property name="successView"><value>reportsGeneralInfo</value></property>

Struts ease of use and complexity of Spring
The Spring-MVC component of Spring seems to receive the most complaints and is considered confusing. This is what I found when introducing this technology to a team of developers. I tend to agree with this statement to a certain extent, as I saw this to be the biggest learning curve in Spring. This may be due to several reasons, one being the development team all had Struts Action framework experience and Struts action controllers and Spring's controllers are different. Struts action controllers are consistent in almost every action where Spring Controllers can vary by definition depending what is required. Struts action controllers essentially has the same methods implemented each time no matter what the functionality requires. This is an easy design pattern to implement. Spring-MVC seems to take another approach. Spring claims to follow excellent design patterns taking the approach “Open for extension, closed for modification.”

Spring Abstraction
This abstract approach will allow for extension or implementation of existing controllers, but disabled and prevented some functionality to prevent bad design. This seems to lead to the comparison of implemented simplicity vs. flexible abstraction. I understand that Spring-MVC even abstract enough to allow other MVC frameworks develop using its interfaces and classes. Spring Web flow utilizes Spring MVC as an abstraction library to handle the flows. This makes Spring-MVC more of a meta-framework. It may be used as a framework to create other frameworks. This may be a good thing from architecture design standpoint, but difficult for development of simple applications.

This is where Struts really suffers. Struts uses XML based validation to validate form fields. It is bad design and programming practice to do any logic within an XML file. I found myself many times having to create custom XML files to perform specific validation for form fields. This is not an appropriate solution. Validation performed is Spring is much better. Java based validation where logic can be performed is the correct way to handle the validating form fields.

Spring MVC selection
To avoid problems, developers should fully understand their options when choosing spring controllers. There are multiple Controllers to choose from, and even when one is selected, multiple functions may or may not be implemented. This creates a larger learning curve and a requires a thorough investigation of each Controller implementation that Spring provides before any decisions should be made.

Struts and Spring
But as Spring allows, you can choose not to use it. Initially people are looking at Spring and compare it to others, like Spring vs. Struts or Spring vs. WebWork, but realization will set in that Spring is an integration tool. The assumption that that Spring MVC has to be used with Spring is false. Spring-MVC does have a larger learning curve than proven technologies like Struts. Understanding that Spring vs. Struts is comparing apples and oranges as Spring has a much larger scope that Struts. The more appropriate comparison is Spring MVC vs. Struts in which either can be used in Spring. In fact, Spring can be used with Struts and Spring MVC be avoided all together.

Subscribe to Syndicate