Monday, August 4, 2008

Working With Struts and Creating Viewable JSP Pages

Working with Struts

Before we can jump into creating our first Struts application, we must establish a baseline of understanding with regards to how the Struts framework works. Earlier discussion of the ActionServlet has broached this topic.

A Struts application works like this:

First, all client requests go through the ActionServlet. We don’t code the ActionServlet, although we do code these things called Action classes, which the ActionServlet is responsible for invoking.

The big question is, given a request, how does the ActionServlet know which Action class to invoke?

Well, the ActionServlet uses the struts-config.xml to map a client request to an appropriate Action object. For example, the struts-config.xml file might contain an entry such as this:


According to this entry, if a client calls the ActionServlet with a URL that ends with /, the ActionServlet will invoke the RegisterAction.

Action objects have a method called execute, or perform if you’re still coding in Struts 1.0. The execute of perform method contains the logic needed to handle the clients request. The execute method of the Action class is where the developer places their code.

Towards the end of the execute method of the Action class, after the business logic required to handle the request is completed, the developer is responsible for figuring out which JSP should be called to generate a web page to be displayed in the clients browser.

The return type of the execute method is something called an ActionForward, which simple put, represents a web page we are interested in forwarding the client. When the web page is sent back to the client device, the request – response cycle is completed.

Let's create a simple Struts application that figures out which language a client speaks, and send them to an appropriate JSP page.

Creating Java Server Pages

The final task of an Action object is to decide what type of a web page, usually an html or jsp, will be returned to the client. We are going to put the cart before the horse by creating our jsp pages before we create our Action.

Our Action class is going to check the preferred language of the client, and send them to an appropriate web page. We’re only going to concern ourselves with French, English, and Spanish speaking clients, demonstrating a general disregard for all other languages.

1. From the ‘Project Navigator’ view of the Web perspective, right click on the WebContent folder and from the context menu, select New à JSP File

2. For the name of the file, enter englishspeaker.jsp

3. Click ‘Finish’ to create the jsp file.

Page Designer, a WYSIWYG editor will appear. Treat Page Designer just like you would Microsoft Word or any other visual editor.

4. Delete the default text “Place content here.” and then add your own text. Something witty about the user speaking English.

5. Press CTRL+S or do a File à Save All to save your changes.

An asterix appears in the editor tab of any unsaved files. When a file is saved, the marking goes away.

6. Create a second and third jsp page, one being creatively called spanishspeaker.jsp and the other called frenchspeaker.jsp.

7. Add witty comments about the French and Spanish languages in the corresponding jsp files.

8. Save your files using a FileàSave All from the menu.

NOTE: Make sure these three files, englishspeaker.jsp, frenchspeaker.jsp and spanishspeaker.jsp, are under the WebContent folder.

It isn't good enough for these files to simply be close to the WebContent folder.

Do not create fancy little subdirectories under the WebContent folder. You can get fancy later. If you want this exercise to work, make sure the three files are under the WebContent folder.

Now, which folder are your three JSP files under?

Once you have saved your JSP files, you are ready to move onto the next step, creating the Action class.

No comments: