Using the scripting framework (javax.script.*), it's really easy to get basic integration between Java and Javascript working. However, once you get to the point of wanting to do something really useful, you run into all the little ways that the bridge from Javascript back to Java (and, to some extent, the other direction too) are messy.
Example: if you've got a Javascript object set up to implement a Java interface (which is basically easy to do), and one of the methods you want to implement needs to return an array of strings, you can't just do this:
// ...
return [ firstString, "hello world", lastString ];
That won't work because Javascript arrays aren't automatically converted to Java arrays. You have to explicitly construct a Java array with reflection, fill it up, and return that.
There are several irritating things like t开发者_StackOverflow中文版hat, and none of them are hard to deal with, really, but as I'm now starting in on the second project that needs those tools I wonder whether this has been solved by some simple Javascript "bridge" framework designed for this purpose. I've found nothing useful via google searches (mostly because any search with the term "java" in it results in a billion hits), but there are people here who seem to know everything so I thought I'd ask.
One way that you can help the problem is using one of the two answers here. I've successfully gotten the Java based solution to work with variables.
The second one can easily be adapted into a JS util function. IE
return toJavaArray(["something",1]);
精彩评论