Written by nitinpai on June 3rd, 2007
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!.
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.