Friday, March 7, 2008

Struts with Me



Struts 2 Features

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.

Other Features:

  • 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 Jakarta assists to create an extensible development environment. However, with the growing demand of web application, Strut 1 does not stand firm and needs to be changed with demand. This led to the creation of Strut2, which is more developer friendly with features like Ajax, rapid development and extensibility.

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, Ajax tags etc. So the Struts communities and the WebWork team brought together several special features in WebWork2 to make it more advance in the Open Source world. Later the name of WebWork2 has changed to Struts2. Hence, Apache Strut 2 is a dynamic, extensible framework for a complete application development from building, deploying and maintaining.

WebWork is a framework for web-application development that has been included in Struts framework 2.0 release. It has some unique concepts and constructs like its compatibility of working within existing Web APIs in Java rather than trying to replace them completely. It has been built specifically taking into account the developer’s productivity and code simplicity. Furthermore it is completely context dependent that provides a wrapper around XWork. When working on web applications the web work provides a context that helps web developer in specific implementations.

While, XWork provides a mechanism that is used for configuration and factory implementation management. This mechanism is dependencies inject mechanism.

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

  1. User Sends request: User sends a request to the server for some resource.
  2. FilterDispatcher determines the appropriate action: The FilterDispatcher looks at the request and then determines the appropriate Action.
  3. Interceptors are applied: Interceptors configured for applying the common functionalities such as workflow, validation, file upload etc. are automatically applied to the request.
  4. Execution of Action: Then the action method is executed to perform the database related operations like storing or retrieving the data from database.
  5. Output rendering: Then the Result render the output.
  6. 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.
  7. Display the result to user: Finally the control is returned to the servlet container, which sends the output to the user browser.














Struts 2 Architecture

Struts 2 is very elegant and flexible front controller framework based on many standard technologies like Java Filters, Java Beans, Resource Bundles, 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.

Exception Handling:

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.
  • Interceptors:
    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.




















Why Struts 2


The new version Struts 2.0 is a combination of the Sturts action framework and Webwork. According to the Struts 2.0.1 release announcement, some key features are:

  • Simplified Design - Programming the abstract classes instead of interfaces is one of design problem of struts1 framework that has been resolved in the struts 2 framework. Most of the Struts 2 classes are based on interfaces and most of its core interfaces are HTTP independent. Struts 2 Action classes are framework independent and are simplified to look as simple POJOs. Framework components are tried to keep loosely coupled.
  • Simplified Actions - Actions are simple POJOs. Any java class with execute() method can be used as an Action class. Even we don't need to implement interfaces always. Inversion of Control is introduced while developing the action classes. This make the actions to be neutral to the underlying framework .
  • No more ActionForms - ActionForms feature is no more known to the struts2 framework. Simple JavaBean flavored actions are used to put properties directly . No need to use all String properties.
  • Simplified testability - Struts 2 Actions are HTTP independent and framework neutral. This enables to test struts applications very easily without resorting to mock objects.
  • Intelligent Defaults - Most configuration elements have a default value which can be set according to the need. Even there are xml-based default configuration files that can be overridden according to the need.
  • Improved results - Unlike ActionForwards, Struts 2 Results provides flexibility to create multiple type of outputs and in actual it helps to prepare the response.
  • Better Tag features - Struts 2 tags enables to add style sheet-driven markup capabilities, so that we can create consistent pages with less code. Struts 2 tags are more capable and result oriented. Struts 2 tag markup can be altered by changing an underlying stylesheet. Individual tag markup can be changed by editing a FreeMarker template. Both JSP and FreeMarker tags are fully supported.
  • Annotations introduced : Applications in struts 2 can use use Java 5 annotations as an alternative to XML and Java properties configuration. Annotations minimizes the use of xml.
  • Stateful Checkboxes - Struts 2 checkboxes do not require special handling for false values.
  • QuickStart - Many changes can be made on the fly without restarting a web container.
  • customizing controller - Struts 1 lets to customize the request processor per module, Struts 2 lets to customize the request handling per action, if desired.
  • Easy Spring integration - Struts 2 Actions are Spring-aware. Just needs to add Spring beans!
  • Easy plugins - Struts 2 extensions can be added by dropping in a JAR. No manual configuration required!
  • AJAX support - The AJAX theme gives interactive applications a significant boost.
    The framework provides a set of tags to help you ajaxify your applications, even on Dojo. The AJAX features include:
    1. AJAX Client Side Validation
    2. Remote form submission support (works with the submit tag as well)
    3. An advanced div template that provides dynamic reloading of partial HTML
    4. An advanced a template that provides the ability to load and evaluate JavaScript remotely
    5. An AJAX-only tabbed Panel implementation
    6. A rich pub-sub event model
    7. Interactive auto complete tag




Thursday, March 6, 2008

My Bihar

Bihar's antiquity is evident from its name, which is derived from the ancient word "VIHARA" (monastery). It is indeed a land of monasteries. Hindu, Buddhist, Jain, Muslim and Sikh shrines abound in this ancient land where India's first major empires rose and fell. Where the ruins of the worlds' earliest university slumbers in the void of time. The passage of Ganga, flowing wide and deep enrich the plains of Bihar before distributing in Bengal's deltoid zone.

Among all Indian states, Bihar is the one most intimately linked to the Buddha’s life, resulting in a trail of pilgrimages which have come to be known as the Buddhist circuit. The Buddhist trail begins at the capital city, Patna, where a noteworthy museum contains a collection of Hindu and Buddhist sculptures as well as a terracotta urn said to contain the ashes of Lord Buddha.

The Khuda Baksh Oriental Library has rare Muslim manuscripts including some from the University of Cordoba in Spain. 40 km away, Vaishali, was the site for the second Buddhist Council is the presence of ruins testify. 90 km south of Patna is Nalanda which translates as the place that confers the lotus’ (of spiritual knowledge). A monastic university flourished here from the 5th to the 11th century. It is said to have contained nine million books, with 2,000 teachers to impart knowledge to 10,000 students who came from all over the Buddhist world. Lord Buddha himself taught here and Hieun Tsang, the 7th century Chinese traveler, was a student. Ongoing excavations have uncovered temples, monasteries and lecture halls. Rajgir, ‘the royal palace’, 12 km south, was the venue for the first Buddhist Council.

The Buddha spent five years at Rajgir after having attained enlightenment, and many of the remains at Rajgir commemorate various incidents related to life of Buddha, the hill of Gridhrakuta being perhaps the most important, as this is where the Buddha delivered most of his sermons. Bodhgaya is the spot where Lord Buddha attained enlightenment, with the Mahabodhi Temple marking the precise location.

This landlocked state of Bihar is surrounded by Nepal, Bengal, Orissa, Madhya Pradesh, Uttar Pradesh and comprises four cultural regions-Bhojpur, Mithila and Magadha and Chotanagpur. Rivers Kosi and Gandak from the north and Sone from the south join the Ganga. In the fertile plains, rice, sugarcane, oilseeds, gram, maize, jute, barley and wheat are cultivated.