...
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">/formformSuccess.jsp</result> <result name="invalid.token" type="dispatcher">/form.jsp</result> <interceptor-ref name="defaultStack"/> <interceptor-ref name="token"/> </action> </package> </xwork> |
...
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.
...
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 our web.xml files)
- WebWork will look up the action formTest in its action hierarchy and call any Interceptors that we might have defined.
- WebWork will translate formTest and decide we would like to call the method processForm in the class com.opensymphony.webwork.example.FormAction as defined in our xwork.xml file.
- Our 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 us accordingly.
Most of the content here provided by Matt Dowell <matt.dowell@notiva.com>