我正在尝试在 React + Spring + Nashorn 中构建一个同构应用,但是,我遇到以下错误:
jdk.nashorn.internal.runtime.ECMAException:ReferenceError:未定义“Map”。
可以采取哪些措施来纠正这个问题?
似乎我提供的细节太少了。我在做的是: 1.我创建了2个js模块:vendor.js(用于node_modules)和app.js(我的应用程序)(使用插件webpack.optimize.CommonsChunkPlugin) 2.定义nashorn-polyfill.js如下:
var global = this;
var window = this;
var process = {env:{}};
var console = {};
console.debug = print;
console.log = print;
console.warn = print;
console.error = print;
创建的类React,它使用Nashorn Javascript Engine加载3个js文件。
@Component 公共类React {
private static final Logger logger = LogManager.getLogger(React.class);
@Value(value = "classpath:static/build/nashorn-polyfill.js")
private Resource nashornPolyfillFile;
@Value(value = "classpath:static/build/vendor.js")
private Resource vendorFile;
@Value(value = "classpath:static/build/app.js")
private Resource bundleJsFile;
public String renderEntryPoint() throws ScriptException, IOException {
NashornScriptEngine nashornScriptEngine = getNashornScriptEngine();
try {
Object html = nashornScriptEngine.invokeFunction("renderServer");
return String.valueOf(html);
} catch (Exception e) {
throw new IllegalStateException("Error! Failed to render react component!", e);
}
}
private NashornScriptEngine getNashornScriptEngine() throws ScriptException, IOException {
NashornScriptEngine nashornScriptEngine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
nashornScriptEngine.eval("load('"+nashornPolyfillFile.getURL()+"')");
nashornScriptEngine.eval("load('"+vendorFile.getURL()+"')");
nashornScriptEngine.eval("load('"+bundleJsFile.getURL()+"')");
return nashornScriptEngine;
}
}
看来行nashornScriptEngine.eval(“load('”+ vendorFile.getURL()+“')”);给出了上述错误
2018-01-19 10:17:59.400 ERROR 1332 --- [nio-8095-exec-2] oaccC [。[。[/]。[dispatcherServlet]:servlet的[Servlet.service()[dispatcherServlet]在con 带有路径[]的文本引发异常[请求处理失败;嵌套异常是javax.script.ScriptException:ReferenceError:文件中未定义“Map” 带有根本原因的/build/resources/main/static/build/vendor.js,行号为11241]
jdk.nashorn.internal.runtime.ECMAException:ReferenceError:未定义“Map” 在jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)〜[nashorn.jar:na] 在jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)〜[nashorn.jar:na] 在jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)〜[nashorn.jar:na] 在jdk.nashorn.internal.objects.Global。 noSuchProperty (Global.java:1441)〜[nashorn.jar:na]
如果您需要更多详细信息,请与我们联系。