Wednesday, August 19, 2009

Working Of Struts 2 Hello World


In my previous post on Getting started with struts 2, i have explained about the configuration of eclipse, the localhost server and the folder structure of a hello world program. Now let me explain the flow of the project and its working of the struts-blank project.

Struts is purely based on the Model- View- Contoller (MVC) design pattern. It is one of the best and most well developed design patterns in use. By using the MVC architecture we break the processing in three sections named Model, the View, and the Controller.

The struts container first of all reads the WEB-INF/web.xml file which instructs the server to use the required filter and mapping conditions. This is the file which has got the welcome list tag. You must specify the file name in the welcome-list tag, whichever you want as the index file for your project.

In struts application we have another xml file which is a Struts configuration file named as struts.config.xml. The name of this file can be changed. The name of the struts configuration file can be configured in the web.xml file. This file is placed under the WEB-INF directory of the web application. It is an XML document that describes all or part of Struts application. In our project it is nothing but the struts.xml file which include the example.xml file. These two xml files acts as the controller. When ever a action is called, the control flow to struts.xml and finds the corresponding class file linked to that action. The class files are the compiled form of java file which are the actual performer in the project. These file contains the getter setter methods and the database handling codes. Generally these java files are called as the Model. Every function in the class has got the SUCCESS and FAILURE, depending upon the result the corresponding jsp files are linked in the controller. Those jsp files are called as View. In the run time controller acts as the interface between the model and view.

In this Hello world, first of all the web.xml file is called, that tells
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter as filter and the index.html as welcome file. In index.html a action is being called, the corresponding class file(java page - model) and the result page(jsp page - view) will be found in the struts.xml file. Now the HelloWorld.java file in the WEB-INF/example folder will be executed because helloworld.action is called from index.html. In HelloWorld.java the string 'MESSAGE' is assigned to a value by retriving it from the package.properties file through getter and setter methods. Then the output of the program is show in the HelloWorld.jsp file as mentioned in the struts.xml.

The same control flow applies for all the Complex project, first try to run this project and understand its flow which will make you feel comfortable when handling complex projects. Lot more example are available in the same pack downloaded from Apache struts. Try all those examples. Get more details and simple explanations on struts from