Thursday 3 May 2007

Struts 1.3 Tutorial.

The previous article gave you the brief outline over how the request is processed within the struts framework. I demonstrated the 5 basic steps. I would now elaborate the entire process with more in-depth details.

The ActionServlet (Our controller)

ActionServlet is the only servlet in entire Struts framework, and is responsible for handling all of the requests. It delegates most of this grunt work to the Request Processor and Action classes. When the controller initializes, it first loads the application config corresponding to the "config" init-param.

<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>

we generally keep the name struts-config.xml and place it within folder WEB-INF. however you may change its name or location .e.g in my application i may
keep the xml within WEB-INF/config folder with the name app-config.xml in that case the path would be like

<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/config/app-config.xml</param-value>
</init-param>


If there are multiple modules then each is identified by the init-param config/modulename e.g. config/foo. For each of these elements, the framework loads the configuration file specified by the value of that init-param. e.g in this case the controller would load /WEB-INF/foo/struts-config.xml for module foo.

<init-param>
<param-name>config/foo</param-name>
<param-value>/WEB-INF/foo/struts-config.xml</param-value>
</init-param>

Let us say there are 3 different modules in your application foo, zoo and hey. When a request is recieved, How the controller determines which module out of 3 will be given the control of processing the request.

To access the module foo, you would use a URL like: http://localhost:8080/myApp/foo/someAction.do
To access the module zoo, you would use a URL like: http://localhost:8080/myApp/zoo/someAction.do etc.

As soon as the request is received by the controller it analyses the the URL to figure out the module once thats done. the controller invokes that module's RequestProcessor. it calls invokes process method of the RequestProcessor. We would discuss the RequestProcessor next in detail.

One last thing about ActionServlet, what do you think about the <load-on-startup> property ? don't you think it should be there?

Yes!!! we define the <load-on-startup>1</load-on-startup> (some valid int value) so that the ActionServlet is initialized along with the container.