Cocoon/Groovy : Howto get the values of parameters from Froms ?
Par foxmask le mardi, juin 8 2004, 13:55 - General - Lien permanent
just a little Howto that explain how to use Groovy with cocoon and get values of parameters you fill in forms !!!
you can also find this post on the wiki of cocoon at GroovyGetFormsParameters page
this post is the next step of my previous one that can be found at GroovyWithSQLDatabaseAccess
a little Howto get the values of a form you just submitted
my form will be composed only with one field ; a select multiple ; to see how to get the choosen values.
the little piece of xsl :
<form action="fox.xml" method="post">
<fieldset><legend>Players</legend>
<select name="player_url_name" multiple="multiple">
<option value="Player1">The is the Player1</option><br/>
<option value="Player2">The is the Player2</option><br/>
<option value="Player3">The is the Player3</option><br/>
</select>
</fieldset>
<fieldset><legend>Valid</legend
<input type="submit" value="ok" name="valid"/>
</fieldset>
</form>
the pipeline in the sitemap.xmap :
<map:match pattern="fox.xml">
<map:generate type="script" src="fox.groovy"/>
<map:serialize type="xml"/>
</map:match>
the groovy script fox.groovy :
import org.apache.cocoon.environment.ObjectModelHelper
import groovy.xml.SAXBuilder
// get the parameters from the form
objectModel = bsf.lookupBean("objectModel");
request = ObjectModelHelper.getRequest(objectModel);
player_url_name = request.getParameterValues("player_url_name");
// Create a Groovy Builder which writes directly to the Cocoon pipeline
contentHandler = bsf.lookupBean("contentHandler")
xml = new SAXBuilder(contentHandler)
// Builder does not seem to call this
contentHandler.startDocument()
xml.root() {
foxyz() {
// loop in the array and make my appropriate node
for ( i in player_url_name ) {
player("${i}")
}
}
}
contentHandler.endDocument()
the generating xml output after choosing all the players :
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<foxyz>
<player>Player1</player>
<player>Player2</player>
<player>Player3</player>
</foxyz>
</root>
Thanks to Bertrand Delacretaz for his help about bsf.lookupBean
- olivier demah -
Commentaires
Olivier Could you please email me - I am trying to work with Groovy & Cocoon, processing XML (instead of XSLT) - I see from the Wiki you are familair with this, and would like to ask some questions... maybe can lead to upgrade of Wiki site?