CSC/ECE 517 Fall 2007/wiki2 1 rl: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 31: Line 31:
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.
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.


Struts' ActionServlet and Rails' DispatchServlet are both examples of the Front Controller pattern; as such, they both provide the same functionality. They accept HTTP requests, parse the URL, and forward processing of the request to an appropriate action. In the case of Struts, an action is a class that extends Action; for Rails, it is a class that extends ActionController. The main difference between the two front controllers is how they determine the action that processes a particular request.
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. Struts, on the other hand, requires an XML configuration file that maps requests to specific controllers and actions.
 
With Struts, a developer needs to externalize the mappings of specific requests to Action classes in an XML configuration file. When the ActionServlet is first loaded, it parses this file and prepares to accept requests. By convention, HTTP requests that end in .do get redirected to ActionServlet for dispatching to the appropriate Action. The XML in Figure 2 is a typical mapping. It tells the ActionServlet to forward a request called deleteOrder.do to controllers.order.DeleteOrderAction for further processing.
 
Rails takes a different approach. Instead of relying upon a configuration file to map requests to actions, it discovers the appropriate action based on the URL requested. As you can see in Figure 2, the URL http://localhost/order/delete/4 indicates for Rails to invoke the delete method on an instance of OrderController and make 4 available as an instance variable. Rails is smart enough to know that /order maps to a controller class defined in a file named order_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.


==Advantages==
==Advantages==

Revision as of 01:06, 23 October 2007

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.

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.

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 Ruby 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. Struts, on the other hand, requires an XML configuration file that maps requests to specific controllers and actions.

Advantages

both have advantages of MVC

Example? advantage: the short and clear URLs, new so not playing catchup on standards (page 2 of book), http://en.wikipedia.org/wiki/Convention_over_Configuration (hibernate) http://www.javalobby.org/java/forums/t65305.html

Advantages for the Developer

  • Scaffolds: Rails applications can take advantage of scaffolds 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.
  • 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.
  • Don't Repeat Yourself: 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.
  • rails claims developers can be 10 times more productive (http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/) http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ : Although I can't guarantee that the framework will deliver on its promise of joy, the statement does a good job of summing up Rails' qualities. The full stack consists of a Web server, a framework for processing HTTP requests and responses, and a framework for easily persisting data to a relational database. Rails strives for development ease by eliminating complicated XML configuration files and using the very dynamic nature of the Ruby language to help minimize much of the repeating code often found in static typed languages.

    Advantages for the Application

    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

    See Also

    1. Hibernate