Woody sample using actions (map:act)
Below we show how the sample from WoodySample can be done using actions. The sourcecode of these actions is included with the Cocoon source code:
MakeFormAction and HandleFormSubmitAction.
<map:match pattern="registration"> <map:select type="request-method"> <map:when test="GET"> <map:act type="woody-make-form"> <map:parameter name="form-definition" value="forms/registration.xml"/> <map:parameter name="attribute-name" value="registrationform"/> </map:act> </map:when> <map:otherwise> <map:act type="woody-handle-form-submit"> <map:parameter name="form-definition" value="forms/registration.xml"/> <map:parameter name="attribute-name" value="registrationform"/> <map:generate type="serverpages" src="forms/registration_success.xsp"/> <map:serialize/> </map:act> </map:otherwise> </map:select> <map:generate src="forms/registration_template.xml"/> <map:transform type="woody"> <map:parameter name="attribute-name" value="registrationform"/> </map:transform> <map:transform type="i18n"> <map:parameter name="locale" value="en-US"/> </map:transform> <map:transform src="resources/woody-samples-styling.xsl"/> <map:transform type="i18n"> <map:parameter name="locale" value="en-US"/> </map:transform> <map:serialize/> </map:match>
How this sitemap works
- based on the request method (GET or POST), we decide whether this is a first-time request of the form (GET) or whether this is a form submit (POST).
- in case of a first-time request, the woody-make-form action will simply create a form instance and put it in a request attribute (this is a storage area that lasts for the duration of the request)
- in case of a form submit, the woody-handle-form-submit action will create a form instance, and let it process the request and validate it. If validation was successful, it returns non-null (so that the contents of the <map:act> element will be executed), otherwise it returns null (so that the pipeline below the map:act will be executed).
- in case of successful validation, we simply execute an XSP page (see example below). In "real-life" situations, this may also call an action, do a redirect, or whathever.
- in case validation failed, or in case the form is displayed for the first time, the pipeline below the selector is executed.
registration_success.xsp
Here's an example of what the XSP could look like.
<?xml version="1.0"?> <xsp:page language="java" xmlns:xsp="http://apache.org/xsp"> <xsp:structure> <xsp:include>org.apache.cocoon.woody.formmodel.*</xsp:include> </xsp:structure> <page> <title>Registration result</title> <content> <xsp:logic> // get reference to form and some of the widgets on it Form form = (Form)request.getAttribute("registrationform"); Field name = (Field)form.getWidget("name"); </xsp:logic> Registration was successful for <xsp:expr>name.getValue()</xsp:expr> </content> </page> </xsp:page>