2015-03-15 18:56题主

Java8在ServerScript中对Java数组采用join方式,报has no such function "join"

错误提示:TypeError: [Ljava.lang.String;@10ebb76 has no such function "join"
源代码中,在Moudle的InitScript中有request.setAttribute("jsRoleArrayText","['"+request.getAttribute("sys.userRoles").join("','") +"']");
------------------------------------------------------------------------------
原因join方法适用于JavaScritp数组,不适用于Java数组。
sys.userRoles是Java数组,因此无join方法。
Java8 ServerScript(Nashorn)采用编译运行方式,Java8之前的ServerScript(Rhino)采用解释运行的方式,Rhino存在隐式转换。

2015-03-16 00:01#1
补充
 InitScript内容:
request.setAttribute("jsRoleArrayText","['"+request.getAttribute("sys.userRoles").join("','") +"']");
Wb.println(request,request.getAttribute("sys.userRoles"));
Wb.println(request,request.getAttribute("jsRoleArrayText"));

Java7下控制台显示:
[Ljava.lang.String;@1edf6a9
['23SKALGNXQ0U','13MQDOSBYDPX','23T8F1APQUTZ','23SKALGNXQ0S']

java8下控制台显示:
javax.script.ScriptException: TypeError: [Ljava.lang.String;@1dc48d9 has no such function "join" in <eval> at line number 1
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(Na
shornScriptEngine.java:455)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:416)
    at jdk.nashorn.api.scripting.NashornScriptEngine.access$300(NashornScriptEngine.
java:70)
    at jdk.nashorn.api.scripting.NashornScriptEngine$3.eval(NashornScriptEngine.java:499)
    at com.webbuilder.common.ScriptBuffer.run(ScriptBuffer.java:41)
    at com.webbuilder.common.Parser.cre
ateModule(Parser.java:345)
    at com.webbuilder.common.Parser.parse(Parser.java:96)
    at com.webbuilder.common.Main.service(Main.java:36)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:72
5)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.cata
lina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.Standa
rdContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(
StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:
610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http1
1.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
    at org.apache.coyote.http11.
Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:285)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2439)
    at org.apache.tomcat.util.net.A
prEndpoint$SocketProcessor.run(AprEndpoint.java:2428)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    a
t org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: <eval>:1 TypeError: [Ljava.lang.String;@1dc48d9 has no such f
unction "join"
    at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:213)
    at jdk.nashorn.internal.runtime.ECMA
Errors.typeError(ECMAErrors.java:185)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:172)
    at jdk.nashorn.internal.runtime.linker.NashornBottomLinker.linkBean(NashornBottomLin
ker.java:103)
    at jdk.nashorn.internal.runtime.linker.NashornBottomLinker.getGuardedInvocation(NashornBottomLinker.java:71)
    at jdk.internal.dynalink.support.CompositeGuardingDynamicLinker.getGuarde
dInvocation(CompositeGuardingDynamicLinker.java:124)
    at jdk.internal.dynalink.support.LinkerServicesImpl.getGuardedInvocation(LinkerServicesImpl.java:149)
    at jdk.internal.dynalink.DynamicLinker.re
link(DynamicLinker.java:233)
    at jdk.nashorn.internal.scripts.Script$Recompilation$7$11$\^eval\_.L:1(<eval>:1)
    at jdk.nashorn.internal.scripts.Script$6$\^eval\_.:program(<eval>:1)
    at jdk.nashorn.
internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:636)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
    at jdk.nashorn.internal.runtime.ScriptRuntime
.apply(ScriptRuntime.java:387)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:414)
    ... 29 more
2015-03-16 12:12#2
在Zhao总的帮助下已经解决:
问题出在javaArray和jsArray上,之前貌似可以自动转换,java8后不行了,只能手动转换一下。
代码如下:
var javaArray=request.getAttribute("sys.userRoles");
var jsArray=[];
for(var index in javaArray)
    jsArray.push(javaArray[index]);
request.setAttribute("jsRoleArrayText","['"+ jsArray.join("','") +"']");
2015-03-17 09:41#3
java8 Nashorn引擎采用编译运行,之前的Rhino貌似解释运行
2015-03-20 09:01#4
昨天群里说加上一行代码:
load("nashorn:mozilla_compat.js");
nashorn就可以兼容rhino
2015-03-27 13:16#5
在ServerScript中默认的原类型是JavaScript类型,如果需要使用Java类型,可以使用诸如java.lang.String(jsString)之类的方法转换。

返回开发者社区