Monday, August 4, 2008

Creating Action Classes and Action Mappings: Struts Development with WebSphere and Rational Application Developer IRAD

Creating Action Classes and Mappings

1. From the ‘Project Navigator’ view of the Web perspective, right click on the ‘Java Resources’ folder and select New à Other…

The ‘New Resource Wizard’ will appear.

2. On the left pane of the ‘New Resource Wizard’ scroll down and open ‘Web’ Under ‘Web’ you will find an option for Struts resources. Click on ‘Stuts’ to bring up the various Struts resources that can be configured.

3. In the right pane, select ‘Action Class’ and click ‘Next >”

4. Enter the following values:

Java package: com.pulpjava.action

Action class name: TryLingualAction

Ensure that the execute method that takes an HttpServletRequest is selected and click ‘Next >’

We need to create mappings for englishpage, spanishpage and frenchpage.

5.Click the ‘Add’ button adjacent to the listings of Forwards. For name, enter ‘englishpage’ For the mapping, entere ‘./englishspeaker.jsp’

Also map:

frenchpage à ./frenchspeaker.jsp spanishpage à spanishspeaker.jsp

6. Click ‘Finish’ to finally create the TryLingualAction class

Coding the Action

Notice that IBM adds some heavily convoluted code that is supposed to make our Struts development a bit easier. It probably does when you get a little bit more familiar with the Struts framework, but it might make things a little bit confusing at first. Let’s try to deal with it.

At the beginning of the execute method, a Struts ActionErrors object is declared to help with error handling.

Also, and ActionForward is declared to remind the developer that the whole point of the execute method is to eventually figure out where to forward the user.

They even add a try...catch block and tell you to //do something here. That is exactly what we are going to do.

Inside of the try{ } block, lets figure out what language the user is speaking and then send them to the appropriate page.

Anything you want to know about the user can be pulled from the HttpServletRequest object, and if you hunt around in the request object for a little while, you'll find a method called getLocale() that we can use to figure out the preferred language of the user. The languages we are concerned with are English , Spanish and French.

Make the appropriate changes to the execute method of the class so that the code matches the the following:

package com.pulpjava.action;

import javax.servlet.http.*;

import org.apache.struts.action.*;

public class TryLingualAction extends Action {

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception {

ActionErrors errors = new ActionErrors();

ActionForward forward = new ActionForward();

try {

String language =


if (language.equals("French")) {

forward = mapping.findForward("frenchpage");


if (language.equals("Spanish")) {

forward = mapping.findForward("spanishpage");


if (language.equals("English")) {

forward = mapping.findForward("englishpage");


} catch (Exception e) {

errors.add("name", new ActionError("id"));


return (forward);



Once you have coded your TryLingualAction class, you have completed all of the code needed to implement a full request – response cycle using the Struts framework:

  • You have created an enterprise application
  • Your enterprise application contains a web module
  • You added Struts support to your web module
  • You created JSP pages to display output to the user
  • You created an Action class that contains an execute method that determines which page to display to a user
  • A struts-config.xml file was configured with an appropriate action-mapping and local forwards.

The next step is to configure a Servlet/JSP server and test our application.

Hibernate Tutorials

Sun Certified Java Associate Mock Exams and Tutorials

No comments: