CSC/ECE 517 Fall 2007/wiki2 1 p23
Ruby on Rails and Apache Struts: A comparison based study
There exist several web application development frameworks in the market. A software developer faces a dilemma regarding which of these to employ to implement his application, based on criteria such as enhanced productivity, complexity, scalability, requirement satisfaction, maintenance, etc. This article aims to compare two such frameworks, Ruby on Rails and Apache Struts, from the perspective of the developer and that of the finished application.
Introduction
Web application frameworks are employed to develop dynamic web pages as well as web applications and services. Generally, web application frameworks comprise libraries compiled into a cohesive software stack. Web applications differ from static web pages in that they allow the generation of dynamic responses through database interaction.
Ruby on Rails as well as Apache Struts are based on what is known as the Model-View-Controller (MVC) architectural pattern. The concept underlying MVC is to separate the presentation layer (view) from the logic and the data model. This ensures that any changes to the user interface will not affect the business logic, and reorganization of the data model will not affect the user's view. Ruby on Rails and the Apache Struts framework are both based on the MVC Architecture.
Problem Statement
"Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?"
Ruby on Rails
Ruby on Rails is a framework that facilitates the development, deployment and maintenance of web applications, written in the Ruby programming language.
Design Paradigms
Rails was designed with the underlying concept of Don't Repeat Yourself (DRY), which emphasizes that every piece of information in the system should be expressed only once. As a result, changes to applications designed with Rails will impact a lot less code than they would in other frameworks.
Another paradigm that Rails is based on is Convention over Configuration (CoC), which means that Rails has defaults for every aspect of the application being developed. Hence, following conventions can lead to application deployment with much less code than would have been written in another programming language. Rails is the first framework to have adopted this paradigm. Ruby also provides scaffolding, an "autogenerated framework for manipulating a model".
Architecture
Rails is designed based on the MVC Architecture. It enforces a structure for the application where models, views and controllers are developed functionally separately, and then knit together during the execution of the application. Incoming requests are sent to a 'router', which computes where the request must be directed to, and how it must be interpreted. This directs the request to a method in the controller (an action), which performs some data manipulation or computation. The data is then rendered as a view to the user.
Structure
Rails' framework consists of the following packages: Active Record, Action Pack, Active Support, Action Mailer and Action Web Service.
Active Record
Rails maps relational database tuples to objects using Active Record: tables map to classes, rows to objects, and columns to object attributes. Active Record facilitates the implementation of the primitive database operations: Create, Read, Update and Delete. It is also the model part that allows the user to define relationships such as belongs_to and has_many.
Apache Struts
Apache Struts is a web application framework for developing web applications in Java.
Design Paradigms
The key technologies underlying the core control layer of the Struts framework are Java Servlets, JavaBeans and XML.
Architecture
Struts achieves separation between data, views and the underlying logic, according to the MVC architecture. It provides the controller (ActionServlet) and the views, which can be generated for the user interface, usually with JSP. The developer must bridge the view and the controller together the model - with a configuration file struts-config.xml. Messaging between components is achieved through JavaBeans.
Like Ruby on Rails, the 'router' receives requests and actions defined in the configuration file determine the processing of the request. The Action class corresponding to this action is called, and it interacts with the model to receive a string (ActionForward) indicating where the results must be directed.
ActionServlet
ActionServlet provides the controller for the framework and performs the following tasks:
- Identifies the action procedure to direct the incoming request to.
- Uses this to map to the corresponding Java action class name (an implementation of the Action or Command interface).
- Populate the field properties of the associated ActionForm bean.
- Call the execute method
Other Features
Struts supports internationalization, and also includes a template mechanism (Tiles). This is achieved by including a library of markup tags.
Comparison and Evaluation
These web application frameworks can be compared against each other based on several criteria, as given below.
Architectural Differences
Both rely on the MVC Architecture. But they differ slightly in the implementation of the front controller. With Struts, a developer needs to externalize mappings of requests to Action classes in an XML configuration file. Rails does not require this mapping, but discovers the mapping based on the URL specified. For example, http://localhost/product/delete/4 indicates that the fourth product must be deleted. Rails is smart enough to know that /product maps to a controller class defined in a file named product_controller.rb. If there is a find method defined in the controller, that method can be invoked by simply replacing delete with find in the URL.
All actions in Struts must be mapped in an XML file. Return values use indirection (Success/Failure) that must be mapped in the XML file. In Rails, this is handled by reflection (the framework figures out how the configuration should look).
Productivity
The Ruby developer is more productive by spending less time writing code. The database migrations enhance productivity by reducing effort spent in switching between schema versions. In the traditional J2EE environment, version control would be required for the developer to achieve the same.
With Rails, one line of code, namely, scaffold :table_name can generate all the CRUD methods required for the application. Achieving the same in the traditional J2EE environment translates roughly to a hundred lines of code.
On the other hand, Apache Struts contains a predefined tag library, to process the content of JavaBeans from the views without the need for any embedded Java code.
Validation in Struts requires the use of "model mirrors" called ActionForms. These mirrors have a 1-1 mapping to your model files a lot of the time, but result in 'repeating yourself'. In Rails, validation occurs in the model. For example:
class MusicCd < ActiveRecord::Base validates_presence_of :title, :artist, :genre, :quantity, :price validates_numericality_of :price validates_uniqueness_of :title protected def validate errors.add(:price,"of the CD should be a positive value")unless price.nil? || price > 0.0 end end
The presentation of validation errors are kept in the Action Controller.
Scalability
Ruby on Rails is often criticized for its poor scalability. Apache Struts is meant for applications that need to be scalable, to a large extent. Apache Struts facilitates the creation of an extensible development environment for enterprise-grade applications, based on industry standards and established design patterns. A very simple application may be structured without the need for Struts, but for a complicated application that need to be maintained over time, Struts offers an ideal solution.
Internationalization
Internationalization refers to the adaptation of computer software to different environments. Struts supports this, whereas Rails currently does not have any explicit support for internationalization, but offers Globalization plugins.
Database Migration
Apache does not contain a DB migration framework like Rails does. The database migrations enhance productivity by reducing effort spent in switching between schema versions. In the traditional J2EE environment, version control would be required for the developer to achieve the same.
Platforms
Considering the platforms these two applications are built upon, a big difference is that Java on the server is targeted at the enterprise. Anyone who wants to run a Java EE application will most likely host their own servers.
Ruby on Rails is targeting a different population: the "Web sites in-between", that are not massively scalable but a fair amount of usage. PHP is not an option since it's installed virtually on 99% of Internet Providers. Hence, Rails being hosted natively is the perfect solution.
Summary
Ruby on Rails is a very new and exciting framework that has generated considerable interest in the Web development community, the core architecture follows the basic patterns found in J2EE. The approach to development of Web applications sets the two frameworks apart.
Rails follows DRY and CoC instead of configuration files like Struts, and the dynamic nature of the Ruby language generates much of the code at runtime. The typical J2EE stack tends to be built from components that are usually developed independently of one another, and XML is often used for bridging components together.
Rails is based upon industry accepted enterprise patterns, and Ruby language allows for fast development, due to its dynamic nature and is extremely popular. At the same time, Struts, based on J2EE, is more established than the relatively-new Rails.
See Also
Agile Web Development with Rails by Dave Thomas and David Heinemeier Hansson, with Leon Breedt, Mike Clark, James Duncan Davidson, Justin Gehtland, and Andreas Schwarz
Model Driven Development with Rails by Antonios Protopsaltou
Starting with Struts2 by Ian Roughley
References
An overview of the Struts framework and Design Patterns and Practices