CSC/ECE 517 Fall 2007/wiki2 1 pp: Difference between revisions
Line 261: | Line 261: | ||
*http://www.megginson.com/blogs/quoderat/2005/06/11/rails-vs-php-mvc-or-view-centric/ | *http://www.megginson.com/blogs/quoderat/2005/06/11/rails-vs-php-mvc-or-view-centric/ | ||
*http://www.oracle.com/technology/oramag/oracle/07-mar/o27rails.html | *http://www.oracle.com/technology/oramag/oracle/07-mar/o27rails.html | ||
== See Also == | == See Also == |
Revision as of 02:20, 30 October 2007
Ruby on Rails vs Apache Struts
Introduction
Apache Struts is an open source web based application framework for developing Java EE web applications. It uses Java Servlet API to extend and develop the Model view controller architecture. The goal of Struts is to separate the database logic from the presentation logic as well as business logic.
Rails is an open source framework for developing database backed web applications in Ruby . The biggest advantage of Ruby is simplicity. Rails takes full benefit of this. While developing an application with Ruby on Rails the programmer is saved from the hassles of writing too many lines of code. Keeping the code small makes it easier to debug and it also increases the readability and maintainability of the code . Ruby on Rails also uses the MVC architecture.
What is MVC?
In complex applications that involve a large amount of data , the programmer might wish to separate the data logic from the user interface logic.
This is where MVC comes in.
Model:
It is used to represent business logic of the application. It indicates the application state and the actions to be performed on that state.
View:
It is used to show the model to the user. It is usually HTML file that provides an interface for the model.
Controller:
Responds to events.It may cause changes to the model.
Advantages Of Struts/Rails to the Application
As both struts and rails are MVC frameworks for developing database backed web applications they have the same advantage from the application point of view.
- Less Coupling-Often one class depends on some other classes for its functionality .This is not good design. Each class should have a specific focus. MVC architecture achieves this by providing separate classes for models,views and controllers.
- Ease Of Change -Business logic resides in both presentation and data access layer. So if there is any change in the business logic one needs to change the code at different layers (presentation database and business layers). This can lead to inconsistent code. Moreover there are chances that the code might be duplicated across multiple places.MVC helps prevent update problems by decoupling logic of all 3 layers.
- Flexibility Of Views – For the same function different views can be generated . For eg the same data can be implemented as applet ,HTML page or a spreadsheet in different applications.
- Modularity increases -In the absence of MVC it would be very cumbersome to remove components from the application or add new components to the application. With MVC modularity increases. So it becomes handy to incorporate new modules into the application.
Advantages Of Rails To The Developer
- Seamless Integration- Rails makes good use of the features of Ruby language.By writing 1-2 lines of code you can create a table ,model and a user interface for the table.
- Migrations-Rails enforces MVC architecture with predefined naming conventions for tables, databases, etc. With Java dealing with database tables as well as objects can be very difficult.
- Convention over configuration- To keep perfect flexibility, Java frameworks maintain massive, pervasive configuration files. Rails rejects this strategy. It assumes a common project directory structure and simple, common naming conventions for methods, classes, tables, and columns . So a rails application will need very less configuration code over a java application
- Immediate Feedback – You can get immediate feedback with Rails. Migrations immediately show you changes after updates to the database.
Advantage Of Struts
- Long Term Advantage-Rails can provide short term advantage over the standard Java APIs . But still it isn’t clear at all whether these advantages continue to persist as the size of the project grows. As struts use Java they can be used very reliably for large scale projects.
Comparison Between Ruby on Rails and Java Struts
Front Controller
Similarities
The controller serves to provide the same core functionality in Ruby On Rails as well as Apache Struts . The controller accepts the incoming Http request , parses the URL of the request ,gets the values of the parameters from the query string and forwards the processing of the request to the appropriate action . The difference in the working lies in the processing of the HTTP request.
Differences
Feature | Ruby On Rails | Apache Struts |
---|---|---|
Base Controller | DispatchServlet | ActionServlet |
Configuration | Rails doesn’t use XML configuration file to store the mapping of HTTP request to an action. It initates the action based on the URL requested. | The mapping of a request to an action is stored in an XML configuration file. When ActionServlet is first loaded it parses the file to begin accepting the incoming HTTP requests. |
Example for Ruby On Rails
http://localhost:3000/Products/add?name=”Bag”
This url tells Rails to invoke the add action of the Products controller class and add bag to the list of products.name is made available to the ProductsController class. Rails figures out that add method should be defined in the ProductsController class.
Example for Apache Struts
http://localhost/addProduct.do?name=”Bag” HTTP requests that end in do are redirected to ActionServlet that initiates appropriate action . <action-mappings> <action path=”/addProduct” Type=”controllers.Product.AddProductAction” Name=”addProduct” Scope=”request” > <forward name=”list” Path=”/listProducts”/> </action>
The above XML configuration file tells ActionServlet to forward the request called addProduct.do to controllers.Product.AddProduct action for further processing of the request. After the product is added the updated product list is shown to the user.
Model and Action
Similarities
In rails as well as struts model is used to interact with the database. It uses the data that the controller gathers to make corresponding changes to the database in a manner that is transparent to the user of the application.
Differences
Feature | Ruby On Rails | Apache Struts |
---|---|---|
Heirarchy | Class ActionController<Base Class ProductsController<ActionController def list end def add end def delete end end |
Class Action { ActionForward execute(ActionMapping am, ActionForm af,HttpServletRequest req, HttpServletResponse res) { } Class AddProductAction extends Action { } Class DeleteProductAction extends Action { } Class ListProductsAction extends Action |
Implementation | The developer needs to extend the ActionController class. Within the extended class you can define actions that process the HTTP request retrieving the parameters from the request. | The developer needs to extend Action class and override execute method of the Action class. Each action denotes a very specific unit of work. The above snippet indicates 3 very specific actions.
AddProductAction, DeleteProductAction and ListProductsAction. The FrontController calls the execute method and passes to it the HttpRequest,HttpResponse ,ActionMapping and Actionform objects. ActionForm is a class that transfers input from the form as well as it can perform validation checks on the input. ActionMapping contains configuration information and maps the request to a specific action. The execute method returns ActionForward object That struts framework uses to determine which component to forward the processing of the request to. |
Concurrency And Synchronisation | Rails creates a new instance of ActionController for each HttpRequest. Developers are freed from the concurrency and synchronization hassles though performance might be adversely affected as a separate Actioncontroller instance is created for each request. | Struts uses multithreading capability of Java. It creates a single instance of the Action and allows multiple threads to access the instance method execute . Because a single object is being used my multiple threads concurrency and synchronization issues might arise. |
Action Classes | ActionController classes are less focused in Rails. They model different units of work as instance methods. | Action classes are very specifically focused in Struts. Each class corresponds to a very specific unit of work/action. |
Sample Code Snippet for delete product action in Java
public class DeleteProductAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { String sid = req.getParameter("id"); // Get the product id from the request Long pid = new Long(sid); deleteProduct(pid); return mapping.findForward("list"); // Forward to ListProducts } private void deleteProduct(Long id) { Session session = HibernateUtil.currentSession(); // Get a session object Transaction tx = null; try { tx = session.beginTransaction(); session.delete(new Product(id)); tx.commit(); } finally { HibernateUtil.closeSession(); } } }
The above example illustrates an Action class for deleting a product from the database in java. The execute method is invoked by FrontController. The method retrieves the parameter product “id” from the HttpServletRequest. It converts id to long and calls deleteProduct method . The deleteProduct retrieves the current session and deletes the product using Hibernate and finally closes the session. The ActionMapping object mapping that is passed to execute is used to find out the next component to which the request is to be forwarded.The component is list which will list the products after a specific product is deleted from the product list.
Sample Code Snippet for delete product action in Ruby
class ProductsController < ActionController::Base def list @products = Product.find_all // def delete id = @params["id"] Product.delete(id) end end
The above example illustrates an ActionController for deleting a product from database in Ruby. Product id is retrieved from the instance hash of parameters. And the record of the product is deleted from the database usinf Product.delete
Persistence Frameworks
Similarities
Persistence frameworks allow developers to map normal objects to relational tables using the least effort. In both ruby as well as java the aim of persistence frameworks is to make interactions with the database as simple as possible.
Differences
Feature | Ruby On Rails | Apache Struts |
---|---|---|
Component Used | Ruby uses ActiveRecord for persistence framework. Active Record is a component that maps an object in ruby to a specific row in the database table. It encapsulates database access. | Struts uses Hibernate for persistence frameworks. In Hibernate a specific class “session” is responsible for transferring the data to and from the database |
Mapping a Table to a Database | No Xml configuration file is needed to map a table to a database. ActiveRecord is able to map Product class to Products table in the database. It also possesses the capability to map the attributes of Products table to instance variables of Product object. So one doesn’t need separate code for getting and setting the attributes related to a database table. | XML configuration file indicates how a class maps to a table in the database. Xml config file as well as class file contains code for setting and getting the attributes associated with the database table. |
Functionality and Elegance
Ease of Use
It is often said that the best thing about Ruby on Rails is, in fact Ruby. Ruby, as a language is easier to use than Java, given its various features like Dynamic typing, built-in inheritors and so on. In addition, Ruby has a steeper learning curve as compared to Java.
Type of Application
Apache Struts is a more general-purpose framework for web application development, while Ruby on Rails focuses on a specific kind of application: new database-linked web applications. It is specifically tuned to make it easier to build such applications faster and in a more elegant manner.
References
- http://en.wikipedia.org/wiki/Apache_Struts
- http://www-128.ibm.com/developerworks/web/library/wa-rubyonrails/
- http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html
- http://www.visualbuilder.com/jsp/struts/tutorial/pageorder/1/
- http://www.roseindia.net/software-tutorials/detail/4483
- http://www.ibm.com/developerworks/java/library/j-cb05096.html
- http://www.megginson.com/blogs/quoderat/2005/06/11/rails-vs-php-mvc-or-view-centric/
- http://www.oracle.com/technology/oramag/oracle/07-mar/o27rails.html