Example
Code Block | ||||
---|---|---|---|---|
| ||||
<?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">/formSuccess.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 | ||||
---|---|---|---|---|
| ||||
<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 | ||||
---|---|---|---|---|
| ||||
<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 | ||||
---|---|---|---|---|
| ||||
<%@ 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> |
The process of events will go as follows:
- WebWork will take notice since the URI ends in .action (defined in web.xml files)
- WebWork will look up the action formTest in its action hierarchy and call any Interceptors that might have been defined.
- WebWork will translate formTest and decide to call the method processForm in the class com.opensymphony.webwork.example.FormAction as defined in xwork.xml file.
- The method will process successfully and give WebWork the SUCCESS return parameter.
- WebWork will translate the SUCCESS return parameter into the location formSuccess.jsp (as defined in xwork.xml) and redirect accordingly.
Most of the content here provided by Matt Dowell <matt.dowell@notiva.com>