Rhino:如何将字符串从 Java 返回到 Javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4050513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rhino: How to return a string from Java to Javascript?
提问by tirithen
How do I use Rhino return a string from Java to Javascript, all I get is org.mozilla.javascript.JavaNativeObjectwhen I use
我如何使用 Rhino 将字符串从 Java 返回到 Javascript,我得到的只是org.mozilla.javascript.JavaNativeObject当我使用
var jsString = new java.lang.String("test");
inside my js file.
在我的 js 文件中。
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.
目标是让 Java 方法返回 String 对象,而不是像上面那样即时创建它。
采纳答案by Greg Hewgill
In general, you would call Context.javaToJSwhich converts a Java object to its closest representation in Javascript. However, for Stringobjects, 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.
通常,您会调用Context.javaToJSwhich 将 Java 对象转换为它在 Javascript 中最接近的表示。但是,对于String对象,该函数返回字符串本身而无需包装它。所以如果你总是返回一个字符串,你不需要做任何特别的事情。
回答by Attila
both will work but try first or go through rhino tutorials https://developer.mozilla.org/en/Rhino_documentation
两者都可以,但先尝试或通过犀牛教程 https://developer.mozilla.org/en/Rhino_documentation
回答by Brian
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!
虽然在大多数情况下,返回的 Java String 类型可以像 JS 代码中的 JS String 类型一样使用,但它没有相同的方法!
In particular I found it cannot be used in a JS object passed to 'stringify()' as it does not have the toJSON() method.
特别是我发现它不能用于传递给 'stringify()' 的 JS 对象,因为它没有 toJSON() 方法。
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:
我找到的唯一解决方案是在 JS 中显式添加“”,将 Java 字符串转换为 JS 字符串。我发现没有办法对 java 方法进行编码以直接返回一个好的 JS 字符串......(因为 Context.javaToJS() 不转换 Java 字符串)例如:
var jstr = MyJavaObj.methodReturningAString();
JSON.stringify({ "toto":jstr}); // Fails
JSON.stringify({ "toto": ""+jstr}); // OK
回答by murkle
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()
对我来说,这是一个 Rhino 错误。JavaScript 中的 s+"" 技巧是有效的,但这里有一个快速补丁可以在 Java 端修复它 - 在 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")
添加此检查以将其转换为原生 JavaScript 字符串(即 typeof 返回“字符串”而不是“对象”)
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(" ", "")
这很重要,否则 s.replace() 调用 Java 版本是错误的,例如 "hell o".replace(" ", "")
回答by Rediska
Turn off the wrapping of Primitives and then the value returned in your expression will be a JS string:
关闭 Primitives 的包装,然后表达式中返回的值将是一个 JS 字符串:
Context cx = Context.enter();
cx.getWrapFactory().setJavaPrimitiveWrap(false);

