Java Struts and Cakephp framework the Difference

This article is designed to throw some comparison between Struts2.x  and cakephp 1.3.x framework, this will also apply for other version of these framework to a great extent, this can be helpful to a cakephp framework developers  to make shift to java struts or vice versa. Difference listed here I resting on what I know please feel free to post a comments.

Both are popular MVC framework and used for different situations a programmer coming from one background will scratch his head trying to figure out why this MVC not doing the work that MVC while there is plenty of architectural difference between two, here i am going to compare them but these points do not conclude anything that one framework is better than other.

1. Configuration -> In struts you will need to define configuration files in the form of web.xml, struts.xml, tiles.xml or hibernate.cfg.xml in case you are using hibernate ORM, in cakephp minimal or no such configuration is required Cakephp has its built on ORM packaged along with main framework.

2. OOPs - All configuration/XML files for example tiles used for creating layouts and struts.xml etc use parent/child or better call object oriented relationship and one configuration can be inherited/extended by other, this feature is absent in cakephp

3. Controller -In cake we have Controller class do doing action specific task  for example app_controller or users_controller same is replaced by Action classes in struts for example UsersAction, it contains all necessary methods. NOTE that cake has few methods namely beforeFilter(), afterRender() that automatically do necessary logic/processing before execution of actual action method, we have parallel option in struts by implementing Preparable interface to that Action class and override its prepare() method.

If no action name are supplied as url parameter to particular controller, in case cakephp a index() method is invoked as default action, in case struts it is execute() method which is supposed to return String like “success” or “error” .

4. Views - In Struts views/jsp objects can access controller method using java beans and display entries obtained from database in case of Cake this is opposite, controller will select views and decide what variables are passed to views.

5. HTTP Request data- For Cake data passed from views to controller as GET or POST parameter is obtained as $this->data (for POST) and $args1, $args2 etc as arguments in controller methods (for GET), in case struts you have corresponding field in Action class to hold that parameter and a pair of setter/getting methods having same name as parameter passed from views, it automatically makes those variables initialized and available for use, this works for both kind of data GET or POST.

6. Auto Magic – Cakephp does some magic stuff  when pulling data from a table, by just knowing table name without entering column info one can get properly mapped array result, in struts when we do manual JDBC or hibernate ORM one has to specify each column names and map it to a variables, these information are kept a XML configuration file. This feature in cakephp has some  drawbacks also.

Leave a Reply

*