最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Rhino: How to return a string from Java to Javascript? - Stack Overflow

programmeradmin2浏览0评论

How do I use Rhino return a string from Java to Javascript, all I get is org.mozilla.javascript.JavaNativeObject when I use

var jsString = new java.lang.String("test");

inside my js file.

Is this the right way to do it?

var jsString = String(new java.lang.String("test"));

The goal is to have a Java method to return the String object instead of creating it on the fly like above.

How do I use Rhino return a string from Java to Javascript, all I get is org.mozilla.javascript.JavaNativeObject when I use

var jsString = new java.lang.String("test");

inside my js file.

Is this the right way to do it?

var jsString = String(new java.lang.String("test"));

The goal is to have a Java method to return the String object instead of creating it on the fly like above.

Share Improve this question edited Nov 12, 2010 at 10:05 Yi Jiang 50.1k16 gold badges138 silver badges136 bronze badges asked Oct 29, 2010 at 9:15 tirithentirithen 3,51711 gold badges43 silver badges67 bronze badges 2
  • Isn't it possible to use a simple string literal? var jsString = "test";? – Jean Hominal Commented Nov 12, 2010 at 10:24
  • if var1 is java.lang.String, simpile to javascript String is : ""+var1 – qxo Commented Aug 15, 2017 at 12:28
Add a comment  | 

4 Answers 4

Reset to default 4

Turn off the wrapping of Primitives and then the value returned in your expression will be a JS string:

Context cx = Context.enter();
cx.getWrapFactory().setJavaPrimitiveWrap(false);

In general, you would call Context.javaToJS which converts a Java object to its closest representation in Javascript. However, for String objects, that function returns the string itself without needing to wrap it. So if you're always returning a string, you don't need to do anything special.

Although in most cases the returned Java String type can be used just like the JS String type within the JS code, it does not have the same methods!

In particular I found it cannot be used in a JS object passed to 'stringify()' as it does not have the toJSON() method.

The only solution I found is to explicitly do the addition of "" in the JS, to convert the Java String to a JS String. I found no way to code the java method to return a good JS string directly... (as Context.javaToJS() doesn't convert a Java String) Eg:

var jstr = MyJavaObj.methodReturningAString();
JSON.stringify({ "toto":jstr});   // Fails
JSON.stringify({ "toto": ""+jstr});  // OK

For me this is a Rhino bug. The s+"" trick inside JavaScript works, but here's a quick patch to fix it Java-side - after this line in NativeJavaMethod.call()

Object retval = meth.invoke(javaObject, args);

add this check to convert it to a native JavaScript string (ie typeof returns "string" not "object")

if (retval instanceof String) {
    return NativeJavaObject.coerceTypeImpl(String.class, retval);
}

This is important otherwise s.replace() calls the Java version so is wrong for eg "h e l l o".replace(" ", "")

https://github.com/mozilla/rhino/issues/638

发布评论

评论列表(0)

  1. 暂无评论