Monday, August 4, 2008

Adding Struts Support to a Web Module in IBMs Rational Application Developer: J2EE/JEE Development Tutorial

Adding Struts Support to a Web Module

J2EE web modules support the Servlet and JSP APIs, but they do not automatically support the Struts framework.

The WSAD provides a very useful wizard that adds struts support to an existing web module.

1. From the ‘Project Navigator’ view of the Web perspective, right click on on the PulpJavaWeb module and select Properties.

Make sure you are right clicking on the web module, as the PulpJava (minus the word Web) project will have different properties from the web module.

In the properties Window, you will see a number of configurable options, including Java Build Path, JavaDoc Location and even a special Struts option. Suprisingly though, in order to add Struts support to a project, you do not go to the struts option. Go figure.

2. In the left hand side, click ‘Web Project Features.’

3. In the workpane on the right, click the checkbox next to Add Struts Support and then click Apply.

4. Accept the default settings, which includes the option to use Struts 1.1, and click Finish. This takes you back to the Properties window for the Web project.

5. Click ‘OK’ to exit the properties window and return to the WSAD.


Explore Struts Support in the Web Module

In the Project Navigator window, open the PulpJavaWeb project and explore its contents.

Under Java Source, note the package named com.ibm.pulpjavaweb.resources and its corresponding file, named ApplicationResources.properties. This is the property file used to hold internationalized text strings.

Open the web content folder and the WEB-INF folder as well and note the various tld, xmi and xml files.

The tld files describe the Struts custom tag libraries.

The xmi files represent IBM specific binding and extensions.

The two xml files are the web.xml file, required by any J2EE web application, and the struts-config.xml file, which is needed by the Struts framework.

These files represent the guts of our Struts based application, and will undoubtedly cause us no end of grief in the future.


Double-click on the web.xml file to open the deployment descriptor editor for the web module.

From there, click on the Servlets tab and take a gander at the single, monolithic Servlet that has been added to your application.

This single servlet, named action, that has been added is an instance of the Sruts ActionServlet.

Click on the action Servlet to display the various associated properties.

A struts application has one Servlet. This one Servlet, the ActionServlet, is responsible for handling every web based request that comes into the Struts application.

Yes, one Servlet to rule them all.

And while you're looking at this ActionServlet, look at the URL mapping for the ActionServlet: *.do

This mapping means that any request that comes to the server that ends in with the three characters ".do" will be sent to the ActionServlet. So, if types into the address bar of their browser:

http://www.pulpjava.com/PulpJavaWeb/login.do

the ActionServlet will handle and appropriately process that request.


No comments: