Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
xml
xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xwork 
          PUBLIC 
          "-//OpenSymphony Group//XWork 1.0//EN" 
          "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
    <include file="webwork-default.xml"/>
    
    <package name="default" extends="webwork-default">

        <interceptors>
            <interceptor-stack name="defaultComponentStack">
                <interceptor-ref name="component"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="defaultStack"/>

        <action name="SimpleCounter" class="com.opensymphony.webwork.example.counter.SimpleCounter">
            <result name="success" type="dispatcher">/success.jsp</result>
            <interceptor-ref name="defaultComponentStack"/>
        </action>

        <!--
          - Velocity implementation of the SimpleCounter.  Also demonstrate a more verbose version of result element
          -->
        <action name="VelocityCounter" class="com.opensymphony.webwork.example.counter.SimpleCounter">
            <result name="success" type="velocity">
                <param name="location">/success.vm</param>
            </result>
            <interceptor-ref name="defaultComponentStack"/>
        </action>

        <!--
            - Different method can be used (processForm). 
            -->
        <action name="formTest" class="com.opensymphony.webwork.example.FormAction" method="processForm" >
            <result name="success" type="dispatcher">/form.jsp</result>
            <result name="invalid.token" type="dispatcher">/form.jsp</result>
            <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="token"/>
        </action>

    </package>
</xwork>

Actions

Code Block
xml
xml

<action name="formTest" class="com.opensymphony.webwork.example.FormAction" method="processForm">

Actions are the basic "unit-of-work" in WebWork, they define, well, actions. An action will usually be a request, (and usually a button click, or form submit). The main action element (tag is too synonymous with JSP) has two parts, the friendly name (referenced in the URL, i.e. saveForm.action) and the corresponding "handler" class.

The optional "method" parameter tells WebWork which method to call based upon this action. If you leave the method parameter blank, WebWork will call the method execute() by default. If there is no execute() method and no method specified in the xml file, WebWork will throw an exception.

Results

Code Block
xml
xml

<result name="missing-data" type="dispatcher">
    <param name="location">/form.jsp</param>
</result>

Result tags tell WebWork what to do next after the action has been called. There are a standard set of result codes built-in to WebWork, (in the Action interface) they include:

Code Block

Action.SUCCESS = "success";
Action.NONE    = "none";
Action.ERROR   = "error";
Action.INPUT   = "input";
Action.LOGIN   = "login";

You can extend these as you see fit. Most of the time you will have either SUCCESS or ERROR, with SUCCESS moving on to the next page in your application:

Interceptors

Interceptors allow you to define code to be executed before and/or after the execution of an action. Interceptors can be a powerful tool when writing web applications. Some of the most common implementations of an Interceptor might be:
Security Checking (ensuring the user is logged in)
Trace Logging (logging every action)
Bottleneck Checking (start a timer before and after every action, to check bottlenecks in your application)
You can also chain Interceptors together to create "packages". If you wanted to do a login check, security check, and logging all before an Action call, this could easily be done with an interceptor package.

For further Reading on Interceptors, see Xwork Interceptors
For further Reading on Xwork configuration files, see Xwork Configuration

Views

WebWork supports JSP and Velocity for your application presentation layer. For this example we will use a JSP file. Webwork comes packaged with a tag library (taglibs). You can use these taglibs as components in your JSP file. Here is an section of our form.jsp page:

Code Block
html
html

<%@ taglib prefix="ww" uri="webwork" %>
<html>
<head><title>Webwork Form Example</title></head>
<body>
   <ww:form name="myForm" action="'formTest'" namespace="/" method="POST">
  <table>
    <ww:textfield label="First Name" name="'formBean.firstName'" value="formBean.firstName"/>
    <ww:textfield label="Last Name" name="'formBean.lastName'" value="formBean.lastName"/>
    <ww:submit value="Save Form"/>
  </table>
</ww:form>
</body>