The Struts Framework – Why choose it?

Struts is a J2EE web application framework created and maintained by Apache Software Foundation Group (ASF). It is a controller framework based on the Front Controller pattern and used to create an MVC (Model View Controller) architecture.

What is a Front Controller Pattern?

A Front controller pattern follows the rule of having a single entity controlling the entire application. The single entity acts as a barrier between the client and the remote application. All requests targeted towards the application and first received by the single entity and then based on the controlling flow of the application, the requests are forwarded to the intended recipient module of the application.

The advantage of the Front Controller Pattern is in the simplicity of maintaining a single entity instead of multiple flows controlled by multiple entities. This results in streamlining the flow and proper filtering of the requests made to the application.

Why is Struts Framework essential in an MVC architecture?

In a J2EE web application, when creating an MVC architecture, usually the JSP is taken for a view while the Servlets do the job of controlling the flow from the view to the underlying model, which may consist of POJO‘s or EJB‘s.

When it comes to designing the controlling flow for the application, developers are left in a scurry to decide how many Servlets should they finalize for the application.This decision is usually based on the number of forms which have to be created for the application to collect the data.

This is because each form when submitted via a JSP with a POST method has to be handled in a Servlet (This can be done in a JSP also, but it is not a good practice since scripting inside a JSP is a criminal offence!) . Now if the application consists of about 10 forms, handling each form in a separate Servlet seems to be a good choice, but that means creating 10 Servlets!.

Any alternatives?

The way to avoid through the above situation is have less Servlets and use the switch or the if..else structure to find which request are you serving for. But over a period of time this nature of creating Servlets can easily lead to creating a mess of conditional statements scattered over the application leading to costly maintainance.

Besides this, the major downside is that as the number of Servlets in your application grows the number of instances to be kept running in the memory also increases. One Servlet spawning many threads is always less heavier than many Servlets having multiple threads.

Why choose the Struts Framework?

Due to the above analogy it might be worth the benefit if having only a Single Controller Servlet which accepts all the requests and then makes the decision of where to forward the request. But, making a manual Front Controller Design may be more time consuming than thought as compared to making multiple servlets.

Hence to avoid all these difficulties, the Struts framework is used to implement the Controller entity in your MVC architecture since it follows the Front Controller pattern. It has been very well establised in the web applications domain for a long time.

What are the advantages of choosing Struts?

The inherent advantage of choosing the Struts Framework is that you get a ready made implementation of the Front Controller pattern. Besides that Struts is a robust and an entirely flexible framework to work with.

Struts gives you the features of

  • Custom class based controlling components
  • Population of Java Beans through HTML form elements and their validation
  • XML based configuration for all underlying components
  • Full support for internationalization through provision of resource bundles for all the HTML elements
  • Tag libraries for easy integration with your existing JSP’s and
  • An elegant way of handling form related errors.

Installing the Struts framework just involves including the required Struts JAR files in your /WEB-INF/lib and taglibs in your WEB-INF directory. Struts is a very robust and lightweight framework which can run even on a Tomcat Web Container. Struts can come handy when you want to achieve the task of making a web application which not only is easier to maintain but also good in its overall performance.

14 thoughts on “The Struts Framework – Why choose it?

  1. And since when does Spring MVC not use a pure servlet??
    Get your facts right. If you want to start a discussion on IoC containers, OK. But just cut the hype.
    And for your interest, Spring is still one of the corporately most used webframeworks in the world.
    With which I’m not saying you should choose it, if you’re lucky enough to find yourself in a green field situation.

  2. SpringMVC *uses* Servlet(s) but *is not* a ‘pure servlet’ solution the author hints on when he says ‘The way to avoid through the above situation is have less Servlets and use the switch or the if..else structure to find which request are you serving for.’

    Also, I wonder why you say Spring is one of the most widely used web frameworks in the world. Spring is widely used as an IOC container, but the web framework part of it is optional, and from what I figure from online discussions and personal experience not nearly as popular as other web frameworks.

  3. I would not agree fully with you Eelco, because I am seeing the increasing use of Spring in application as I have too seen many online discussions and the demand for IOC based approach.

    But I am not saying that Struts is out of the way. It is probably the most stable framework which has been used over many years but Spring is being used since it does not put many dependencies as Struts does and that it also has components spanning over all the tiers in an application. Maybe thats the reason for its success.

    Could you provide me your input on your statement that Spring is not widely used and which frameworks apart from Struts/ Spring are currently used for the web?

  4. Spring is widely used. SpringMVC (an optional subproject of spring) not nearly as widely. I use Spring, but not SpringMVC (and thank god not Struts either).

  5. Well sorry for the confusion, had a little slip of the tongue or in this case keyboard

    When typing :
    And for your interest, Spring is still one of the corporately most used webframeworks in the world.

    I actually meant:
    And for your interest, Struts is still one of the corporately most used webframeworks in the world.

    As in stated in several comments of course 😉
    And for what it’s worth you only have one servlet in a typical struts application (not considering your jsp’s of course 😉 ).

  6. > And for your interest, Struts is still one of the corporately most used webframeworks in the world.

    Ah, yeah, that makes sense 🙂

    > And for what it’s worth you only have one servlet in a typical struts application (not considering your jsp’s of course

    Yep. That’s actually true for many frameworks, including Apache Wicket. But you typically don’t have to deal with that servlet directly with such frameworks.

  7. What do you guys think about Struts 2. I havent tried it yet, but I have heard that it has improved a lot and taken a bit of functionalities from webwork.

  8. @nitinpai – Struts 2 is much more simpler and cleaner than Struts 1, in my opinion. There is no more form bean with this, just Action class together with getters and setters. Setting up is also a breeze in Struts 2, you can either use annotations or the old xml.

    The only downside ive seen so far is there are soo few tutorials and samples than Struts 1. I really dont know why. 🙁

    Btw, ive made a sampe war, exhibiting Struts 2 + Json. Hope this helps:

    http://stikiflem.wordpress.com/2008/08/27/struts-2-json-sample/

Leave a Reply

Your email address will not be published. Required fields are marked *