David Legg wrote:
I'm trying to do something in flowscript that must be pretty common. I
simply want to pass a HashMap of arguments from JavaScript to a Java
method which will then process the Map to perform some useful business
function. Can anyone spot what I'm doing wrong? I get a Javascript
exception (listed at the end of this email).
I suspect I'm not casting the HashMap properly but my JavaScript is weak
in this area. I think I'm doing something similar to the 'Calling Java'
documentation (http://cocoon.apache.org/2.1/userdocs/flow/java.html) but
maybe I missed something.
++++++++
My Flowscript looks something like this: -
importClass(Packages.com.MySingleton);
function main() {
var theParams = new java.util.HashMap();
theParams.put("param1", "value1");
So here the value for the key param1 is the String "value1"...
var usefulObject = MySingleton.doSomethingUseful(theParams); <==
Exception occurs here.
cocoon.sendPage("apage.jx", {"useful" : usefulObject} );
}
++++++++
My Java method is defined as follows: -
public synchronized UsefulObject doSomethingUseful(Map params) {
String[] vals = (String[])params.get("param1");
...and here you're expecting the value for the key param1 to be an
*array* of Strings. That explains the ClassCastException.
So either you must expect a single String as the value, or set it as a
String array in the flowscript. Creating Java arrays in JS is a bit
odd, you have to use reflection: http://www.mozilla.org/rhino/faq.html
Hope that helps.
if (vals == null || vals.length < 1)
...
++++++++
When I access the page I get the following error: -
java.lang.String
org.apache.avalon.framework.CascadingRuntimeException:
"file:/D:/projects/.../flow/myflowscript.js", line 6: uncaught
JavaScript exception: at main
(file:/D:/projects/.../flow/myflowscript.js, Line 6):
java.lang.ClassCastException: java.lang.String
cause: java.lang.ClassCastException: java.lang.String
full exception chain stacktrace[hide]
org.apache.avalon.framework.CascadingRuntimeException:
"file:/D:/projects/.../flow/myflowscript.js", line 6: uncaught
JavaScript exception:
at main (file:/D:/projects/.../flow/myflowscript.js, Line 6):
java.lang.ClassCastException: java.lang.String
at
org.apache.cocoon.components.flow.javascript.fom.FOM_JavaScriptInterpreter.callFunction(FOM_JavaScriptInterpreter.java:764)
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]