CSC/ECE 517 Fall 2007/wiki2 1 rl

From Expertiza_Wiki
Jump to navigation Jump to search

Note to reviewers:

All prose on this page is original. If you doubt this, please indicate the section that you feel is not original and the source from which you believe it was directly copied. Thank you.

Assignment

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?

Introduction

Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.

MVC Architecture

MVC separates an application into three components: the model, the view, and the controller. The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data. The view generates the user interface, which allows the user to view and interact with the data in the model. The controller takes input from the outside world, interprets the input, and manipulates the model accordingly. Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.

Ruby on Rails

Ruby on Rails, also known as "Rails" or "RoR", is the standard Web application framework for the Ruby programming language. It is an open source project that was released in 2004, and was created to make web application development quick and easy.

All Rails applications use the MVC architecture. Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby). The Ruby router directs requests to a particular controller and action based on the URL.

For more basic information on Ruby on Rails, visit the Ruby on Rails homepage.

To see sample Ruby on Rails applications, visit Applications Created with Ruby on Rails.

Apache Struts

Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications. It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture. Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views. The model is left up to the developer.

For more basic information on Apache Struts, visit the Apache Struts homepage.

To see sample Struts applications, visit Struts Applications.

Comparison of Ruby on Rails and Apache Struts

Rails and Struts are similar in many ways. They are both web application frameworks for their respective languages, and they both implement the Model-View-Controller architecture. The figure below, taken from Ruby on Rails and J2EE: Is there room for both?, provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.


As you can see, the two stacks are fundamentally quite similar. Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts. This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not. In addition to not providing a Model implementation, Struts also does not fully provide a View. As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.


In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL. For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb. Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions. This typifies another key difference between Rails and Struts: Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files. Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.

Advantages

Both Rails and Struts offer all the advantages that come with using the MVC architecture. The separation of concerns makes the code more maintainable, reusable, and understandable. It allows developers to specialize, and facilitates parallel development. Finally, it makes enhancements to the application easier to design and implement. The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.


Advantages for the Developer

Rails advantages for the developer

  • Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in seconds.
  • Rails provides database scaffolding out of the box. Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries. This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.
  • Rails favors Convention over Configuration. Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today. For example, if you create a model class called Product, Rails will also generate for you a controller class named ProductsController, and will assume that your model is using a database table called products. You only need to configure the table name if you are using a different value. Writing less code and doing less configuration saves developers time during design, development, test, and maintenance.
  • Rails has Don't Repeat Yourself as its mantra. Information about the system should be expressed in only one place. For example, the names of the columns of a database table need only be contained in the table itself. Rails gathers this information through reflection and discovery. Avoiding code repetition is always good for the developer.
  • Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc. Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.
  • Rails natively supports layouts for a common look and feel. Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.


Struts advantages for the developer

  • With Struts, you are not locked into a specific model implementation. You can select a model implementation that best suits your application's needs.
  • Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.
  • Commercial support is more widely available in the Java community.10
  • When comparing popular job search sites, employment opportunities for Struts are of an order of magnitude greater than those for Rails.11, 12, 13, 14


Advantages for the Final Application

Rails advantages for the final application

  • URLs in a Rails application are short and clear. They are of the form http://host:port/controller/action/id. Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.
  • Some of the virtues of Ruby on Rails, such as a smaller code footprint, more elegant code, zero configuration files, and Rails' DRY mantra make maintenance easier since there is less code to maintain, and it is more easily understood.
  • Since Rails includes almost everything you need for a web application (and there are packages readily available that include the missing pieces), there are fewer moving parts, leading to simpler designs, and therefore, again, a more maintainable solution.


Struts advantages for the final application

  • Struts is an older and more established framework, and is a proven technology.
  • Java has been shown to be faster than Ruby in many performance benchmark tests.15
  • Though Struts itself does not offer a persistence framework, it is often paired with Hibernate. Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.16
  • Complementary technologies related to scalability (fast web servers, clustering, etc.) have evolved significantly in the last two years for RoR, but do not yet reach the maturity level of corresponding technologies for Java/Struts.
  • Rails offers no support for internationalization.17
  • Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.

Conclusion

Rails and Struts each have their own strengths and weaknesses. Rails lends itself to simple designs, rapid development, and improved maintainability. Struts excels in areas of performance, scalability, and internationalization. If one is trying to make an architecture choice for a project, many other considerations must be also taken into account, such as staffing, support, and other economic issues beyond the scope of this technical review.

References

  1. Ruby on Rails and J2EE: Is there room for both?
  2. Rolling with Ruby on Rails Revisited
  3. Ruby on Rails
  4. Model-view-controller
  5. CSC517 Lecture 12: Ruby on Rails
  6. Agile Web Development with Rails
  7. Ruby on Rails Wikipedia entry
  8. Apache Struts Wikipedia entry
  9. Rails vs Java Struts
  10. Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite
  11. Monster.com - Job postings for Struts
  12. Monster.com - Job postings for Rails
  13. Dice.com - Job postings for Struts
  14. Dice.com - Job postings for Rails
  15. The Computer Language Benchmarks Game
  16. Hibernate vs. Rails: The Persistence Showdown
  17. Internationalization in Ruby on Rails
  18. Agile Web Development with Rails
  19. Applications Created with Ruby on Rails
  20. Struts Applications

See Also

  1. Hibernate
  2. SAIF
  3. Apache Tiles