Architecture & DesignWorking with the WebWork Framework

Working with the WebWork Framework

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Currently, the Java development front is split into two main areas: Web development and Swing client-side GUI development. The Swing developers use the excellent Swing toolkit and core Java APIs for the majority of their GUI development needs. Java Web developers also use core Java APIs, but in addition they use frameworks. Most of the enterprise-level, Web-based projects use some sort of an architecture that includes at least one framework. Why? This is because frameworks greatly simplify development and design of the project, decrease time to production, reduce amount of labor and code needed, and improve application performance. I’ve described several major frameworks in my previous article “Modern Frameworks for Web Development.”

This article will concentrate only on WebWork. I will show how to set up WebWork using enterprise-scale tools such as JBuilder 2005 and BEA WebLogic and, as an example, create a sample application that uses some of WebWork’s features. I will assume you have WebLogic (or a comparable application server) installed and configured and are familiar with JBuilder IDE. Because WebWork is J2EE-compliant, other application servers, such as Apache Tomcat can be used as well.

WebWork

Every framework for Web developments by design serves a specific purpose. It could be a Model-View-Controller implementation with Inversion of Control logic for coordination of client to server requests and actions on the server, separation of business logic from view layer, or Java to SQL mapping to abstract interaction with database layer. Also, it could be client data validation and view abstraction via custom framework tag libraries and JSTL (Java standard tag libraries).

WebWork is a Model-View-Controlled type of framework. This means it separates logic from view and simplifies Web development by using a powerful “action” concept on the server side.

It is built on top of a command pattern framework API called XWork. WebWork specific features include: dispatchers that handle/delegate requests; result types that support several view technologies (JSP, Velocity, JasperReports, XML, FreeMarker); and a small but powerful library of JSP tags and Velocity macros. Dispatchers invoke specified XWork actions that access and manipulate the model and provide easy access for the view to display model data.

Setup Server Environment in JBuilder

To get started with WebWork, download it here: http://www.opensymphony.com/webwork/download.action.

In JBuilder 2005, configure an enterprise server to point to your BEA WebLogic installation. If you are using a different app server, the setup should be similar. The WebLogic domain and server should be set up and are beyond the scope of this article. Skip this section if you already have configured any application server in you IDE.

  1. Choose Enterprise|Configure Servers.
    The Configure Servers dialog box appears. (See below)
  2. Select the WebLogic server 8.x by clicking it in the pane on the left. The right side of the dialog box lists the default settings for the selected server.
  3. Check the Enable Server check box at the top of the dialog box. Checking this option enables the fields for the selected application server. You won’t be able to edit any fields until it is checked. The Enable Server check box also determines whether this server will appear in the list of servers when you select a server for your project using the Servers page of the Project|Project Properties dialog box.
    JBuilder provides default settings for the selected server. If JBuilder’s default settings are not appropriate, edit any of the settings as needed. Some servers require you to enter settings in addition to the defaults. Clicking the OK button in this dialog box when not all required values are set or selecting another server while the current one is enabled displays a message dialog box that informs you about the missing settings.
  4. If you want to change any of the settings, click the ellipsis (…) button next to the field and make your changes. Assuming you installed WebLogic Platform 8.1 into a BEA_HOME directory with the name bea, your home directory should be [drive]:/bea/weblogic81/server.
  5. Click the Custom tab to view fields unique to the server. Change or fill in these fields:
    • Home Directory: The BEA Home Directory is the directory into which you installed the WebLogic Server and that contains the registry.xml file. JBuilder assigns a default value for the BEA Home Directory that varies depending on the version of the WebLogic Server installed.
    • JDK Installation Directory: For WebLogic Platform 8.1, the directory is <beahome>/jdk142_05.
    • Domain Directory: Use the ellipsis (…) button to select a directory as your domain directory. The domain directory is the directory where your WebLogic domain is stored. For WebLogic 8.1, it is user_projects/domains/mydomain. Setting the domain directory sets the working directory value on the General page. For more information about WebLogic domains, see your WebLogic documentation.
    • User Name: The user name you use to authenticate yourself to the server.
    • Password: The password you use to authenticate yourself to the server.
    • Server Name: The name you specify is used as a VM parameter for starting the server. JBuilder suggests a default name of “myserver.”
    • Listen Address: Specifies a listen address for this server. The host value must be either the DNS name or the IP address of the server. If you do not specify a Listen Address, the server uses either the machine’s DNS name or its IP address.
    • Listen Port: Specifies the non-SSL listen port for this server. If you do not specify a Listen Port, the server uses 7001 as the default.
    • Version: Should be SP4 at least or higher
    • Add An Admin Console Item To The Enterprise Menu: Checking this check box adds the WebLogic Admin Console item to JBuilder’s Enterprise menu. You must also fill in the next field, Web Browser Path, to have the menu item added.
    • Web Browser Path: Specify the path and file name of the Web browser of your choice. For example, you might specify c:/program files/Internet Explorer/iexplore.exe if you want to use Microsoft’s Internet Explorer.
    • Add A Configuration Wizard Item To The Enterprise Menu: Adds a WebLogic Configuration Wizard item to the JBuilder Enterprise menu. When you select this menu item, the BEA WebLogic Configuration wizard begins, which guides you through the steps of configuring the domain for your use.
    • Use External Compiler: Leave default

Set up the project with WebWork

To show the WebWork framework in action, I created a sample project “WebWork” in JBuilder. To the project libraries (under project properties), I added WebWork jars. The main jar as of this writing is version 2.1.7 I placed and all other libraries in WebWorkLibs and WebWorkOptionalLibs folders.

In the project, I added a package called “webwork_test” and there I created an action class HelloAction that extends ActionSupport. ActionSupport is a parent class that all action classes must extend and implement the “execute” method. This action class will be invoked when the client initiates a request with the action name mapped in the configuration file (more on this later). The class simply has two data String variables—greeting and name—and getter and setter methods for them. In the execute method, greeting is set and name is checked because its value will come from the client’s request.

package webwork_test;
import com.opensymphony.xwork.ActionSupport;
public class HelloAction
   extends ActionSupport {
   String greeting;
   String name;
   public String execute() throws Exception {
      greeting = "Aloha! ";
      if ( (name == null) || (name.length() == 0)) {
         return ERROR;
      }
      return SUCCESS;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getGreeting() {
      return greeting;
   }
}

In addition to the package, I created a Web archive (WAR) module “WebWorkWebModule” in JBuilder, using the Web module wizard under the File -> New menu.

In addition to the standard J2EE Web application configuration XML files, WebWork uses several config XML property files to coordinate requests, validate data, and manage data flow. The main ones are xwork.xml and valuators.xml. They need to be put in the servlet container root path for the framework to see them at runtime.

I placed these files in the project root directory on the hard drive and added them in JBuilder using properties of the Web module.

Right-click on the new Web module. Select the properties’ open content section and use the Add Files button to add these files to the build process.

The xwork.xml defined my action and which class to use to perform it, and what to do in the event of success or failure.

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
   <!-- Include webwork defaults (from WebWork JAR). -->
      <include file="webwork-default.xml" />
      <!-- Configuration for the default package. -->
      <package name="default" extends="webwork-default">
         <!-- Default interceptor stack. -->
         <default-interceptor-ref name="defaultStack" />
         <action name="helloAction"
                 class="webwork_test.HelloAction">
            <result name="error"
                    type="dispatcher">index.jsp</result>
            <result name="success"
                    type="dispatcher">success.jsp</result>
         </action>
      </package>
</xwork>

I took the valuators.xml from the WebWork distribution. Please see the attached project for its source.

To configure the main controller/dispatcher of the framework, I had to edit the standard web.xml file created with the Web module by JBuilder and located under the WEB-INF directory.

The final result looked like this:

Note: The main WebWork dispatcher servlet mapping and the reference to tag libraries and mapping of all actions URL pattern to the servlet URL.

<web-app>
   <display-name>My WebWork Application</display-name>
   <servlet>
      <servlet-name>webwork</servlet-name>
      <servlet-class>com.opensymphony.webwork.dispatcher.
                         ServletDispatcher</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>webwork</servlet-name>
      <url-pattern>*.action</url-pattern>
   </servlet-mapping>
   <taglib>
      <taglib-uri>webwork</taglib-uri>
      <taglib-location>
         /WEB-INF/lib/webwork-2.1.7.jar
      </taglib-location>
   </taglib>
</web-app>

After the module was created, I placed the WebWork tag libraries descriptor TLD file (that comes with the distribution) in the WEB-INF directory,

Now I was done with the configuration!

To test the framework in action, I created two JSP files: index.jsp and success.jsp. They both use the WebWork tag libraries to work with data presentation and flow.

Below is the source for the index jsp. I indicated that my action is helloAction in the form action parameter and created an input field with name “name“. If you recall, the action class looks for this variable in its execute() method.

Index.jsp

<html>
<head>
<title>WebWork in Action</title>
</head>
<body>
<p>Click the button below to activate HelloAction.</p>
<form action="helloAction.action" method="post">
<p>
   <p><input type="text" name="name"/><input type="submit" /></p>
</p>
</form>
</body>
</html>

The success.jsp is more interesting because it uses WebWork tag libs and gets values from the response.

<%@ taglib uri="/WEB-INF/webwork.tld" prefix="ww" %>
<html>
<head>
<title>WebWork in Action result</title>
</head>
<body>
<ww:property value="greeting" /><ww:property value="name" />
</body>
</html>

Here is the working version

Conclusion

In this article, I have shown how to set up and configure a project with the WebWork framework. I have also shown a sample application that uses actions and accesses an action’s property using tags. This approach can (and should) be expanded when developing your applications. The WebWork framework provides a powerful mechanism for building robust Web applications that I only began to discuss here. The framework has a lot more features to offer for building robust Web applications. If you are looking for a Java Web development framework, you definitely should consider WebWork.

Download the source code here.

References

Wikipedia online: http://en.wikipedia.org/wiki/

http://e-docs.bea.com/workshop/docs81/doc/en/core/index.html

WebWork: http://www.opensymphony.com/webwork/

About the Author

Vlad Kofman is a Senior System Architect working on projects under government defense contracts. He has also been involved with enterprise-level projects for major Wall Street firms and the U.S. government. His main interests are object-oriented programming methodologies and design patterns.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories