Comparison of MVC Implementation Between J2EE Struts and ASP

Embed Size (px)

Citation preview

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    1/15

    Comparison of MVC implementation between J2EE Struts 2 & ASP.NET MVC 2, who is the best? Part 2

    ContentsSo, whats the agenda? ................................................................................................................................. 1

    In case you are new to ASP.NET MVC and J2ee Struts framework .............................................................. 2

    Overall Comparison with framework ............................................................................................................ 2

    Automation using template .......................................................................................................................... 3

    Passing data from controller to view ............................................................................................................ 4

    Readymade folder structure ......................................................................................................................... 6

    Strong type views for intellisense ................................................................................................................. 7

    Helper classes.............................................................................................................................................. 10

    URL customization and action mapping ..................................................................................................... 11URL validation ............................................................................................................................................. 12

    Security ....................................................................................................................................................... 13

    Final conclusion ........................................................................................................................................... 14

    Thanks Vishwa............................................................................................................................................. 14

    So, whats the agenda?

    In our previous article we had compared MVC implementation for J2ee and ASP.NET without using

    frameworks; to view comparison click http://www.codeproject.com/KB/aspnet/compjavaaspnet.aspx(

    ASP.NET MVC and J2ee MVC) .

    In todays world no one implements MVC without help of frameworks. So its either ASP.NET orj2ee,

    framework support is a must. Struts 2 has been the most accepted framework in J2ee while in ASP.NET

    the ASP.NET MVC template is the king. In this article we will compare these frameworks in terms of

    how they differ in implementation and their positive and negative points.

    In this article we will compare both frameworks using 8 agenda points

    Automation Passing data between controller to views URL customization Security Folder structure management Intellisense support

    http://www.codeproject.com/KB/aspnet/compjavaaspnet.aspxhttp://www.codeproject.com/KB/aspnet/compjavaaspnet.aspxhttp://www.codeproject.com/KB/aspnet/compjavaaspnet.aspx
  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    2/15

    HTML automation (helper classes).Do watch our .Net interview questions and answers video from this link.NET interview questions and

    answers, you can also catch out J2ee Design pattern videos from this linkJava J2ee Design pattern.

    In case you are new to ASP.NET MVC and J2ee Struts framework

    In case you are not aware of the frameworks you can see the below videos to just get a quick start in

    both the frameworks

    Simple ASP.NET MVC video which displays a hello world.

    http://youtu.be/KAKxm4eQP24?hd=1

    Simple J2EE struts video which shows how to display a simple hello world.

    http://www.youtube.com/watch?v=7rT6duL16Io

    Overall Comparison with framework

    Before even I start below is a full comparison sheet which gives an overall view of the comparison. The

    same we will be discussing in more detail as we proceed in the article.

    Comparison points Microsoft MVC 2 template MVC using J2ee framework (Struts 2)

    Template Automation In built with Visual studio Need to use open source plug in likehttp://mvcwebproject.sourceforge.net

    Passing data from

    controller to view.

    New session management

    techniques like viewdata

    and tempdata are

    introduced.

    Uses the same request object inherently

    using same request interceptor.

    Readymade folder

    structure

    The MVC template creates a

    readymade logical folder

    structure for view, controllers

    and model.

    Does not have a logical folder structure

    like ASP.NET MVC, but can be created

    manually.

    Strong typed views for

    intellisense

    Has string typed view support

    for better intellisense.

    Achieved by using type casting.

    Helper classes Helper classes Tag libraries

    URL customization and

    action mapping

    Uses behind code. Uses the Struts 2 XML file.

    URL validation URL validation can be done

    easily using regex.

    Currently no inbuilt mechanism but can be

    achieved by customized coding.

    Security Has readymade security

    attributes by which we can

    Currently no inherent feature but can be

    achieved by customized coding.

    http://www.questpond.com/http://www.questpond.com/http://www.questpond.com/http://www.questpond.com/http://www.questpond.com/java/javaj2eedesignpattern.htmlhttp://www.questpond.com/java/javaj2eedesignpattern.htmlhttp://www.questpond.com/java/javaj2eedesignpattern.htmlhttp://youtu.be/KAKxm4eQP24?hd=1http://youtu.be/KAKxm4eQP24?hd=1http://www.youtube.com/watch?v=7rT6duL16Iohttp://www.youtube.com/watch?v=7rT6duL16Iohttp://mvcwebproject.sourceforge.net/http://mvcwebproject.sourceforge.net/http://mvcwebproject.sourceforge.net/http://www.youtube.com/watch?v=7rT6duL16Iohttp://youtu.be/KAKxm4eQP24?hd=1http://www.questpond.com/java/javaj2eedesignpattern.htmlhttp://www.questpond.com/http://www.questpond.com/
  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    3/15

    avoid cross site , sql injection.

    Automation using template

    The first thing which caught our eyes is the Microsoft VS IDE which has a nice readymade template from

    where developers can start. In MVC J2ee framework the core struts framework does not have

    something inherent as Microsoft MVC has it.

    Said and done that it does not mean J2EE community is lagging; you can still take help of open source

    plug-in to get the template fromhttp://mvcwebproject.sourceforge.net. Below is a simple snapshot of

    MVC web project template.

    The only difference its not inherent like Microsoft MVC.

    http://mvcwebproject.sourceforge.net/http://mvcwebproject.sourceforge.net/http://mvcwebproject.sourceforge.net/http://mvcwebproject.sourceforge.net/
  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    4/15

    Conclusion: - Definitely Microsoft wins here in terms of more user friendly interfaces and automation

    due to the MVC template. In j2ee struts framework we need to hunt for a third party plug which will

    help us to achieve the same kind automation.

    Passing data from controller to view

    In MVC one of the most crucial parts is transferring data from controller to view. Microsoft MVC

    introduces a new variable called as ViewData which will help us to transport data between controllerand view as shown in the below code snippet.

    The below code snippet sets data in the controller.

    ViewData["CurrentTime"] = DateTime.Now.ToString();

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    5/15

    The below code snippet displays data on the view.

    J2ee Struts framework uses the HTTP request object to pass data from controller to the view. The below

    code snippet sets a simple date value to the request object using the setAttribute function. In ASP.NET

    MVC we cannot change the request object.

    request.setAttribute(CurrentTime,new Date());

    Later we can display the data using getAttribute.

    Conclusion: - First thing both the technologies have the facility of passing data, but somehow j2ee Struts

    framework thought process looks more convincing. At the end of the day view gets a HTTP request, so

    its more logical to pass data using the request objects rather than creating a new variable like view data

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    6/15

    for the same.

    Many ASP.NET MVC fans (which includes me) can also argue logically that the request object is created

    by using the data sent by the end user i.e. from the browser. This data should not be allowed to be

    changed in between by the web application code. The request object should only be allowed to be

    modified by the end user using POST and GET.

    For this I will give equal point to both of them for now.

    Readymade folder structure

    In ASP.NET MVC the template creates a readymade folder structure (they are termed as areas) which

    gives a developer a clear picture of where to store the model, views and controllers as shown in the

    below figure.

    In j2EE struts framework we do not have the clear cut vocabulary for folders as we have in ASP.NETMVC. In j2ee framework the controller and model lies in Java resources folder, while the views are saved

    in web content folder as show in the below diagram.

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    7/15

    Said and done you can always manually rename and create different folder structure to have the same

    logical representation as we have in ASP.NET MVC , only that its not automated.

    As per our knowledge the above logical structure is not possible currently by using any J2EE struts plug-

    in either.

    Conclusion: - ASP.NET MVC has a slight advantage in terms of better project management due to

    readymade logical folder structure, while in J2ee framework we need to create them manually.

    Strong type views for intellisense

    In ASP.NET MVC you have a nice option where you can create strongly typed view. In other words when

    you add a view you can select the model with which this view will connect.

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    8/15

    Later when you go in the view and type model keyword you can get the properties of the object in a

    strongly typed manner.

    In java we do not have the concept of strongly typed view. If you want you can set the object in

    request.getAttribute and then to a type cast to get the object intellisense.

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    9/15

    Conclusion: - This feature can look very exciting for ASP.NET community but it was bit amusing for my

    J2ee friends (I think they are right in lot of sense also). The whole purpose of strong typed views in

    ASP.NET MVC is for better intellisense which we can be achieved by type casting data from view data

    object as shown in the below figure.

    The biggest problem here is that developers can start thinking that the model is tied up the view. We

    understand the intention is not that, but the dialog box just makes a visual intention of doing the same.

    The end goal of MVC is to separate the model from the view.

    So concluding its a good feature to get maximum from less code but just that it can be confusing for

    junior developers who are working on MVC. For a small automation I hope we do not end with a logical

    confusion about MVC fundamental.

    Equal points again to both. I am not giving an extra point to ASP.NET MVC as view thing is more

    confusing and can be achieved by typecasting.

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    10/15

    Helper classes

    A good MVC framework will always provide helper classes to create HTML code for views.

    In ASP.NET MVC we have the helper classes. For instance to create a simple HTML form in ASP.NET MVC

    you can use the HTML helper class as shown in the below code.

    Enter customer id :-

    Enter customer code :-

    Enter customer Amount :-

    In j2ee struts framework we have tag libraries which help us to generate the HTML code as it is done by

    using ASP.NET MVC html helper classes.

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    11/15

    Conclusion: - Both the frameworks have HTML helper classes. Lets not get in to which library is better

    or else we will lose focus on our main comparison. So even points to both the framework on this

    discussion.

    URL customization and action mapping

    MVC is all about actions and these actions are mapped to URL. As a developer you would love to see

    your MVC framework have the capability of customizing and configuring the MVC URL with action

    mapping. Below is a simple table which explains why developers would expect customization andconfiguration for MVC URLs.

    Incoming URL Controller Action Description

    http://www.questpond.com/LocateProduct SearchProductController Search This action

    will help us to

    get all

    products.

    http://www.questpond.com/LocateProduct

    /100

    SearchProductController Search(1001) This action

    will help us to

    get all

    products with

    code 1001.

    http://www.questpond.com/Create MaintainProductController Create This action

    will help us to

    create a new

    product.

    http://www.questpond.com/Modify/1001 MaintainProductController Modify(1001) This action

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    12/15

    will help us to

    modify

    product with

    product code

    1001.

    In ASP.NET MVC this is achieved by using the inbuilt routing mechanism. In order to configure routes

    you can go to the global.asx.cs code and use the routes collection to map the URL structure with the

    controllers and actions.

    routes.MapRoute(

    "HelloWorld", // Route name

    "Pages/RegisterAction/{1}", // URL with parameters

    new { controller = "Register", action = "RegisterAction", id =

    UrlParameter.Optional }); // Parameter defaultsIn order to configure MVC URL in J2ee struts framework we can use the Struts XML file to the same. The

    below mapping is more clean than the routing collection of ASP.NET MVC. In J2ee framework we can see

    the mappings more better as they are mapped directly to page names.

    /pages/UI.jsp

    /pages/UI.jsp

    /pages/success.jsp

    Conclusion: - J2ee Struts framework definitely wins in terms of MVC URL configuration and mapping to

    the controller as its defined using XML file. This thing can be really improved in ASP.NET MVC

    framework. To change the mapping compiling code is more of a burden.

    URL validation

    In ASP.NET MVC we have the advantage of doing URL validation using regex (regular expression) before

    the action hits the controller. For instance below is a simple validation where before the view customeraction is called we can check that the input to the action is only numeric value with 2 digits.

    routes.MapRoute(

    "View", // Route name

    "View/ViewCustomer/{id}", // URL with parameters

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    13/15

    new { controller = "Customer", action = "DisplayCustomer", id

    = 0 }, new { id = @"\d{1,2}" }

    );

    Currently J2ee struts framework does not support the same, said and done you can always still do

    validation after hitting the controller using some custom logic.

    Conclusion: - This is definitely a plus point for ASP.NET MVC because MVC URLs can be invoked from

    any medium, via browser, via URLs etc. So we would like to ensure that appropriate validation is

    checked much before it hits the controller.

    Security

    MVC URLs are mapped to action and they can be invoked directly which also means that they aresubjected to cross site attacks, sql injection etc. ASP.NET MVC framework has provided security

    attributes by which we can protect our URL from such attacks.

    For instance to prevent forgery attacks and cross site scripting attacks we can use the

    HtmLAntiForgeryToken() as shown in the below code snippet.

    In the actions later you can then check if there was any violation.

    [HttpPost] [ValidateAntiForgeryToken]

    public ActionResult Delete(int customerid)

    {

    }

    You can also mark the controller by using validate input attribute to avoid CSS.

    [ValidateInput(true)]

    public class OurController : Controller

    Critical functions on which you do not want actions to be invoked you can mark the methods as

    nonaction as shown in the below code snippet.

    [NonAction]

  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    14/15

    public void DoPasswordChange(string username, string newpassword)

    {

    /* Rest of code unchanged */

    }

    In J2ee Struts framework currently we do not have any inherent security function to achieve the same.Some customized code.

    Conclusion: - This is the biggest win for ASP.NET MVC framework security. Hope that J2ee struts

    framework in coming times has such kind of inherent security feature which will be a great extra added

    advantage for the framework.

    Final conclusion

    Below is the final conclusion. ASP.NET MVC framework out performs J2ee in 4 things while J2ee has the

    flexible XML URL customization which is currently not available in ASP.NET MVC. For all the other points

    both of them remain on the same page.

    I have tried my level best to put forward both the sides and while doing so I never had in my mind that I

    am an ASP.NETMicrosoft MVPand I should hard sell ASP.NET MVC framework. I have made by best

    effort to make a true comparison and see which one of them is the best. I do understand how every

    developer loves his technology, by any chance I have hurted.BIG SORRY.

    Thanks Vishwa

    https://mvp.support.microsoft.com/profile=F15802B5-02D5-4979-BFBE-22A01ED0C2B4https://mvp.support.microsoft.com/profile=F15802B5-02D5-4979-BFBE-22A01ED0C2B4https://mvp.support.microsoft.com/profile=F15802B5-02D5-4979-BFBE-22A01ED0C2B4https://mvp.support.microsoft.com/profile=F15802B5-02D5-4979-BFBE-22A01ED0C2B4
  • 8/3/2019 Comparison of MVC Implementation Between J2EE Struts and ASP

    15/15

    Special thank to Mr Vishwanathan Naryan who helped me to give inputs on the J2EE side without which

    I would not have achieved the same. You can see his Java and j2ee design patterns videos by clicking on

    Java J2ee Design patternvideos.

    http://www.questpond.com/java/javaj2eedesignpattern.htmlhttp://www.questpond.com/java/javaj2eedesignpattern.htmlhttp://www.questpond.com/java/javaj2eedesignpattern.html