The strut-2 framework is designed for the compilation of the entire development cycle including of building, developing and maintaining the whole application. It is very extensible as each class of the framework is based on an Interface and all the base classes are given an extra application and even you can add your own. The basic platform requirements are Servlet API 2.4, JSP API 2.0 and Java 5.
Some of the general features of the current Apache Strut 2 framework are given below.
Architecture – First the web browser request a resource for which the Filter Dispatcher decides the suitable action. Then the Interceptors use the required functions and after that the Action method executes all the function like storing and retrieving data from a database. Then the result can be seen on the output of the browser in HTML, PDF, images or any other.
Tags - Tags in Strut 2 allow creating dynamic web application with less number of coding. Not only these tags contain output data but also provide style sheet driven markup that in turn helps in creating pages with less code. Here the tags also support validation and localization of coding that in turn offer more utilization. The less number of codes also makes it easy to read and maintain.
MVC – The Model View Controller in Strut 2 framework acts as a co-coordinator between application’s model and web view. Its Controller and View components can come together with other technology to develop the model. The framework has its library and markup tags to present the data dynamically.
Configuration – Provides a deployment descriptor to initialize resources and is in XML format. The initialization is taken place by scanning all the classes using Java packages or you can use an application configuration file to control the entire configuration. Its general-purpose defaults allow using struts directly Out of the box.
Configuration files are reloadable that allows changes without restarting a web container.
- All framework classes are based on interfaces and core interfaces are independent from HTTP.
- Check boxes do not require any kind of special application for false values.
- Any class can be used as an action class and one can input properties by using any JavaBean directly to the action class.
- Strut 2 actions are Spring friendly and so easy to Spring integration.
- AZAX theme enables to make the application more dynamic.
- Portal and servlet deployment are easy due to automatic portlet support without altering code.
- The request handling in every action makes it easy to customize, when required.
Struts 2 History
Apache Struts is an open-source framework that is used for developing Java web application. Originally developed by the programmer and author Craig R. McClanahan this was later taken over by the Apache Software Foundation in 2002. Struts have provided an excellent framework for developing application easily by organizing JSP and Servlet basically based on HTML formats and Java code. Strut1 with all standard Java technologies and packages of
Strut is a well-organized framework based on MVC architecture. In Model-View-Architecture, Model stands for the business or database code, the View represents the page design code and the Controller for navigational code. All these together makes Struts an essential framework for building Java application. But with the development of new and lightweight MVC based frame works like Spring, Stripes and Tapestry, it becomes necessary to modify the Struts framework. So, the team of Apache Struts and another J2EE framework, WebWork of OpenSymphony joined hand together to develop an advanced frame works with all possible developing features that will make it developer and user friendly.
Strut2 is a combined features of Struts Ti and WebWork 2 projects that advocates higher level application by using the architecture of WebWork2 with features including a plugin framework, a new API,
Struts 2 Architecture
Struts and webwork has joined together to develop the Struts 2 Framework. Struts 2 Framework is very extensible and elegant for the development of enterprise web application of any size. In this section we are going to explain you the architecture of Struts 2 Framework.
Request Lifecycle in Struts 2 applications
- User Sends request: User sends a request to the server for some resource.
- FilterDispatcher determines the appropriate action: The FilterDispatcher looks at the request and then determines the appropriate Action.
- Interceptors are applied: Interceptors configured for applying the common functionalities such as workflow, validation, file upload etc. are automatically applied to the request.
- Execution of Action: Then the action method is executed to perform the database related operations like storing or retrieving the data from database.
- Output rendering: Then the Result render the output.
- Return of Request: Then the request returns through the interceptors in the reverse order. The returning request allows us to perform the clean-up or additional processing.
Struts 2 Architecture
Struts 2 is very elegant and flexible front controller framework based on many standard technologies like Java Filters, Java Beans, ResourceBundles, XML etc.
For the Model, the framework can use any data access technologies like JDBC, EJB, Hibernate etc. and for the View, the framework can be integrated with JSP, JTL, JSF, Jakarta Velocity Engine, Templates, PDF, XSLT etc.
The Struts 2 Framework allows us to define exception handlers and inceptors.
- Exception Handlers:
Exception handlers allows us to define the exception handling procedure on global and local basis. Framework catches the exception and then displays the page of our choice with appropriate message and exception details.
The Interceptors are used to specify the "request-processing lifecycle" for an action. Interceptors are configured to apply the common functionalities like workflow, validation, etc.. to the request.
Struts 2 Architecture
The following diagram depicts the architecture of Struts 2 Framework. Following diagram shows the the initial request goes to the servlet container such as tomcat, which is then passed through standard filer chain.
Image: Struts 2 Architecture
The filter chain includes:
- Action ContextCleanUp filter:
The ActionContextCleanUp filter is optional and it is useful when integration has to be done with other technologies like SiteMash Plugin.
Next the FilterDispatch is called, which in turn uses the ActionMapper to determine weather to invoke an Action. If the action is required to be invoked, the FilterDispatcher delegates the control to the ActionProxy.
The ActionProxy takes the help from Configuration Files manager, which is initialized from the struts.xml. Then the ActionProxy creates an ActionInvocation, which implements the command pattern. The ActionInvocation process invokes the Interceptors (if configured) and then invokes the action. The the ActionInvocation looks for proper result. Then the result is executed, which involves the rendering of JSP or templates.
Then the Interceptors are executed again in reverse order. Finally the response returns through the filters configured in web.xml file. If the ActionContextCleanUp filter is configured, the FilterDispatcher does not clean the ThreadLocal ActionContext. If the ActionContextCleanUp filter is not present then the FilterDispatcher will cleanup all the ThreadLocals present.
In this section we have learned about the Architecture of Struts 2 Framework.
Download Struts 2.0
In this section we will download and install the Struts 2.0 on the latest version of Tomcat container. We will first download tomcat and configure it as our development server. Then we will download Struts 2.0 and install the struts-blank application on the tomcat server server to check the examples that comes with the struts-blank application.
Downloading Struts 2.0
Visit the Struts download site http://struts.apache.org/download.cgi and download the Struts 2.0 for this tutorial.
Download the TomcatDownload the latest version of tomcat from http://tomcat.apache.org/download-55.cgi. We have downloaded apache-tomcat-5.5.20.zip for this tutorial Extract downloaded file and run the C:\apache-tomcat-5.5.20\bin\startup.bat to start the tomcat. Type http://localhost:8080/ in your browser, it should show the welcome page in the browser window Now we will install the struts-blank application on the tomcat container and test the application.
Extracting Struts 2.0 distribution and installing on tomcat
Extract the downloaded struts distribution struts-2.0.6-all.zip into your favorite directory. To install the struts blank application copy "struts2-blank-2.0.6" from "
Feb 25, 2007 11:42:23 PM org.apache.coyote.http11.Http11BaseProtocol start
To test the struts-blank application type http://localhost:8080/struts2-blank-2.0.6 in the browser and the struts-blank application get displayed in your browser window.
Click the English link and familiarize yourself with Struts Blank sample application.
Struts 2.0 distribution also contains the following samples applications that you can try:
You can simply copy these files to the webapps directory of your tomcat server. Tomcat will automatically deploy these application and then you can test these applications.