Description

To write an XSP Action you need to declarate the use of the built-in logicsheet action. In the header of your XSP please include:

xmlns:action="http://apache.org/cocoon/action/1.0"

Allowed Tags

Here is a description of all the tags and the parameters it uses:


Writing an action using XSP

This is some basic information that explains how to write an action using XSP. It might not be clean or perfect but it did work for Litrik

When you write your action in XSP, Cocoon will notice when the file has changed and recompile automagically.

Sitemap

Declaring the Action component

Your sitemap should contain the following declaration of the action component:

<map:actions>
  <map:action 
    logger="sitemap.action.xsp-action" 
    name="xsp-action" 
    src="org.apache.cocoon.acting.ServerPagesAction"/>
</map:actions>

Note: Some other pieces of documentation use serverpages as the action name instead of xsp-action. I prefer to use xsp-action to avoid confusions of what it defines. Note that the name serverpages is already used by the XSP Generator. This is why I dont use it.

How to use in a Pipeline

And of course your sitemap should contain a pipeline that actually contains an action written in XSP. To use it we must use the <map:action>. The value of the type attribute must be xsp-action and the value of the src attribute will contain the full path to the XSP file.

<map:match pattern="test-of-xsp-action">
  <map:act type="xsp-action" src="my-xsp-action.xsp">
    <!-- This part is called when the action executes <action:set-success/> -->
    <map:read src="{my-result-filename}" mime-type="image/gif"/>
  </map:act>
  <!-- This part is called when the action executes <action:set-failure/> -->
  <map:generate type="serverpages" src="error.xsp"/>
  <map:serialize/>
</map:match>

Writing the Action

The src attribute of the <map:act> of the pipeline contains my-xsp-action.xsp. That is the XSP file that is actually implementing the action logic.

This is what my-xsp-action.xsp could look like:

<?xml version="1.0"?>
<xsp:page
  language="java"
  xmlns:action="http://apache.org/cocoon/action/1.0"
  xmlns:xsp="http://apache.org/xsp"
>
<dummypage>

  <xsp:logic>
  try
  {
                
    if( /* do some test here */ )
    {
      /* With <action:set-result/> we set return values
      ** that can be used in the calling pipeline */
      <action:set-result name="my-result-filename" value="GoodMorning.gif"/>
      <action:set-result name="some-other-result" value="5987"/>

      // You can use action:param to set the name and/or the value dynamically.
      <action:set-result>
        <action:param name="name"><xsp:expr>getName()</xsp:expr></action:param>
        <action:param name="value"><xsp:expr>getValue()</xsp:expr></action:param>
      </action:set-result>
    }
    else if( /* do some other test here */ )
    {
      <action:set-result name="my-result-filename" value="GoodAfternoon.gif"/>
    }
    else
    {
      throw new Exception() ;
    }

    // With <action:set-success/> we indicate that
    // everything went just fine
    <action:set-success/>
  }
  catch(Exception e)
  {     
    e.printStackTrace() ;
    // With <action:set-failure/> we indicate that
    // something went wrong and that the action has failed
    <action:set-failure/>
  }
  </xsp:logic>

</dummypage>

</xsp:page>

XSPAction (last edited 2009-09-20 23:39:58 by localhost)