📄 javascript.xtp
字号:
<s1 title="JavaScript API"><summarylist/><p>Resin's JavaScript engine is in the com.caucho.es package. The mainclasses are com.caucho.es.parser.Parser and com.caucho.es.Script.Parser is a factory for generating scripts and Script is acompiled script.</p><p>Running JavaScript is a two-step process: compile the scriptand then execute it.</p><s2 title='Basic Scripts'><p>Basic javascript just needs two know about two classes:com.caucho.es.parser.Parser andcom.caucho.es.parser.Script.Parser is a factory for parsing Scripts and Script represents acompiled script.</p><p>Most application will set the script path and the work directory.The script path specifies the directories to search for javascriptfiles. MergePath is handy since it lets you build a list ofdirectories to search. You can also add the classpath to the list ofdirectories to search by calling addClassPath. The default searchpath is the current directory of the process (e.g. server-home) andincludes the classpath. So a simple application can just putjavascript files in the classpath.</p><p>The work directory specifies where the generated *.java and*.class will go. The parser will try to load a precompiled scriptbefore parsing the script. If any of the source files have changed,the parser will recompile the script.</p><example title='Compiling a Script'>package com.caucho.vfs.*;package com.caucho.es.*;...com.caucho.es.parser.Parser parser = Resin.getParser();MergePath scriptPath = new MergePath();scriptPath.addMergePath(Vfs.lookup("/home/ferg/js"));ClassLoader loader = Thread.currentThread().getContextClassLoader();scriptPath.addClassPath(loader);parser.setScriptPath(scriptPath);Path workPath = Vfs.lookup("/tmp/caucho/work");parser.setWorkDir(workPath);Script script = parser.parse("test.js");</example><p>Once the script is compiled, the application can execute it. TheScript object is thread-safe, since all the JavaScript objects,including the global objects, have their own copy for each execution.</p><p>Custom global objects can be configured by adding them to aHashMap in the script call.</p><example title='Executing a Script'>Bean myBean = ...;HashMap properties = new HashMap();Path pwd = Vfs.lookup(".");properties.put("File", pwd);script.execute(properties, myBean);</example></s2></s1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -