Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Hello World the WebWork way

Create your first WebWork action

An action is a piece of code that is executed when a particular URL is requested. After actions are executed, a result visually displays the outcome of whatever code was executed in the action. A result is generally an HTML page, but it can also be a PDF file, an Excel spreadsheet, or even a Java applet window. In this book, we'll primarily focus on HTML results, because those are most specific to the Web. As Newton's Third Law states, "every action must have a reaction." (An action doesn't technically have to have a result, but it generally does.) Although not "equal and opposite," a result is always the reaction to an action being executed in WebWork.

Suppose you want to create a simple "Hello, World" example in which a message is displayed whenever a user goes to a URL such as http://localhost/helloWorld.action. Because you've mapped WebWork's servlet to *.action, you need an action named helloWorld. To create the "Hello, World" example, you need to do three things:

  1. Create an action class: HelloWorld
  2. Create a result: hello.jsp
  3. Configure the action and result

Let's begin by writing the code that creates the welcome message.

Saying hello, the WebWork way

Start by creating the action class, HelloWorld.java, as shown in the listing below.

HelloWorld.java

 package ch2.example1;

import com.opensymphony.xwork.Action;

public class HelloWorld implements Action { private String message;

public String execute() { message = "Hello, World!\n"; message += "The time is:\n"; message += System.currentTimeMillis(); return SUCCESS; }

public String getMessage() { return message; } }


The first and most important thing to note is that the HelloWorld class implements the Action interface. All WebWork actions must implement the Action interface, which provides the execute() method that WebWork calls when executing the action.

Inside the execute() method, you construct a "Hello, World" message along with the current time. You expose the message field via a getMessage() JavaBean-style getter. This allows the message to be retrieved and displayed to the user by the JSP (JavaServer Pages) tags.

Finally, the execute() method returns SUCCESS (a constant for the string "success"), indicating that the action successfully completed. This constant and others, such as INPUT and ERROR, are defined in the Action interface. All WebWork actions must return a result code—a string indicating the outcome of the action execution.

Note that the result code doesn't necessarily mean a result will be executed, although generally one is. You'll soon see how these result codes are used to map to results to be displayed to the user. Now that the action is created, the next logical step is to create an HTML display for this message.

Displaying output to the Web browser

WebWork allows many different ways of displaying the output of an action to the user, but the simplest and most common approach is to show HTML to a Web browser. Other techniques include displaying a PDF report or a comma-separated value (CSV) table. You can easily create a JSP page that generates the HTML view:

1 | 2 | 3 | 4 | 5 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. Hello World the WebWork way
By JavaWorldAdministrator
1 04/22/08 06:03 AM
by Anonymous


Resources