By Justin Freeman
Over the years in my work as a consultant, I’ve worked with several companies that were looking to convert their legacy systems to a web-based solution. Once they selected Java (great choice!) as their language, their next question is usually, "What Java framework should we use?"In my experience, it usually comes down to Struts 2 or SpringMVC. I have worked extensively with both of these frameworks, so I will compare and contrast them for you. Keep in mind that there really isn’t a definitive “correct” framework to choose. You should look at your options, the dynamics of your development team and what you are trying to accomplish before you choose a framework. I’m hoping that this will help guide you in your decision making.
Spring is basically a Java framework that allows you to create complex web applications through simple components and configurations. There are other aspects of Spring that you can use independently of SpringMVC, such as dependency injection and aspect-oriented programming. For the purposes of this article, we don’t need to talk about those because they can be used with SpringMVC or Struts 2. In my experience, they work really well with either.
SpringMVC is part of the Spring framework that is used for handing and processing web-based requests. It follows the basic MVC (Model-View-Controller) pattern. Spring-created JavaBeans are the model. The controller is the DispatcherServlet, which handles all requests and passes them to the corresponding controller. A nice feature is that you can use annotations to handle this requirement instead of using the infamous application-context.xml file. Anyone who used Spring in the early days knows that this file can get large quickly. Breaking this context file into smaller files just makes it more complicated. Anyway, the last piece is the view, which is usually composed of JSPs. However, if you are developing webservices the view could be a number of things. We will come back to webservices later, but this is your basic layout of Spring and its components.
Struts 2 is also a Java request-based framework that allows you to create web applications. The Struts 2 framework follows the basic MVC pattern. The controller is called the ActionController. It is backed by mappings in a struts.xml file that allows requests to be forwarded to the appropriate action. In my experience, most people use EJBs or even Spring JavaBeans as their model. However, the view is important here because Struts 2 has its own view language. This is an advantage over SpringMVC in that you can just learn Struts 2 and you can build a Java framework from beginning to end, which helps with your development team’s learning curve. I have found that companies looking to ramp up quickly will go the Struts 2 route because it is easier to get up and running.
Now that we have a basic understanding of the frameworks, let's compare and contrast the frameworks using a few of the web components and possible issues you would come across while learning and developing using these frameworks.
Ajax has become a good technology for displaying dynamic content or field validation for web-based applications. In my work as a consultant, I have been fortunate to work with Ajax in both SpringMVC and Struts 2 environments. Struts 2 has built-in Ajax support so there is no need to import a third-party library. So if you are keeping score, Struts 2 has its own view language, controller and Ajax support. Again, once your learn Struts 2 you can pretty much develop a web application from beginning to end without the assistance of almost any other framework.
Although SpringMVC does not have native Ajax support, this just gives a more seasoned developer the ability to choose its Ajax library. This is especially important since your development team may already have knowledge or a preference for an Ajax library, allowing you to leverage that knowledge. For example, in one company that was switching to a SpringMVC environment, many of the developers were familiar with the Dojo Ajax library. So instead of switching to Struts 2 and learning a new Ajax library, they were able to use all the knowledge of Dojo and make the full learning curve of switching to SpringMVC a little easier. I would give the edge to Struts 2 here because it’s easier to hit the ground running, provided that your developers do not have any experience with Ajax libraries.
RESTful Webservices Support
In any request-based web application, it is becoming increasing popular to use RESTful webservices because they are easier to develop and maintain and startup costs are minimal. In a world where IT is seen more as a cost than an expense, this is pretty important. Most developers I have come across don’t know how RESTful webservices work; they just know how to write them. It has really become that simple.
In Struts 2, I found it a little difficult to set up RESTful webservices when I first learned it. We essentially needed 2 separate xml files (one for RESTful URLs and the re-direct and the second for the struts action mapping). Another hassle is that in your Java Action you don’t necessarily know that this is a RESTful webservice or if it is just a plain old Struts Action. This can be confusing to some less experienced developers.
In SpringMVC, RESTful webservices take advantage of annotations which not only makes webservices easier to identify, it also makes them easier to develop and maintain. The majority of your changes all happen in the Java class and you just have to touch one xml file once as opposed to Struts 2 where you need to edit 3 different places for adding or removing webservice. I really like the RESTful webservice support in SpringMVC with an assist from annotations because everything is one place and therefore development time is shorter.
Continuing Maintenance and Evolving Framework
Java in general is constantly evolving and its frameworks are no different. They are both open source so improvements and efficiencies are always on the horizon. I find that Struts 2 is a more mature technology in that the changes to the basic framework have been minimal. SpringMVC, and along with it Spring, is constantly changing and getting better. The issue is that your developers will have to constantly keep up with the technology in order to improve the application as Java grows, web browsers change and other improvements happen around the webspace. It only sounds daunting because most IT firms do not take this into account. They only care about fixing bugs and enhancing the application. Eventually, old technologies will die off and new technologies and techniques that make web applications perform more efficiently will replace them. In this regard, SpringMVC applications must be constantly monitored for improvements and upgrades are a must. This is less important for Struts 2 because the underlying framework is not changing much. So, something else to think about when choosing between SpringMVC and Struts 2 is the maintenance costs and how much your department can afford.
These are just some of the things you can use when deciding between SpringMVC and Struts 2. Like I said in the beginning, there isn’t really a definitive answer to which one is better. However, in my opinion, I would say if you are looking for a stable framework you should choose Struts 2. If you are looking for a more robust framework, you should choose SpringMVC.