java编程中实现调用js方法分析

时间:2022-02-27 18:31:51

本文实例讲述了java编程中实现调用js方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
* 加载脚本引擎,并在java中调用js方法
*/
public void test2()
{
     ScriptEngineManager manager = new ScriptEngineManager();
     ScriptEngine engine = manager.getEngineByName("javascript");
     try {
       String str="2&1";
       Double d = (Double) engine.eval(str);
       Integer i=d.intValue();
       System.out.println(i);
     } catch (ScriptException ex) {
      ex.printStackTrace();
     }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
* 在java中调用js,jdk1.6中有加载js引擎类,然后由它来调用js方法。
* 并通过JDK平台给script的方法中的形参赋值
*/
public void test(String name)
{
    ScriptEngineManager sem = new ScriptEngineManager();
    /*
     *sem.getEngineByExtension(String extension)参数为js
     sem.getEngineByMimeType(String mimeType) 参数为application/javascript 或者text/javascript
     sem.getEngineByName(String shortName)参数为js或javascript或JavaScript
     */
    ScriptEngine se = sem.getEngineByName("js");
    try
    {
     String script = "function say(){ return 'hello,'"+name+"; }";
     se.eval(script);
     Invocable inv2 = (Invocable) se;
     String res=(String)inv2.invokeFunction("say",name);
     System.out.println(res);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
* 演示如何将java中对象作为js中全局变量,同时演示将file类赋给脚本语言,并获得其属性。
*/
public void testScriptVariables()
{
    ScriptEngineManager sem=new ScriptEngineManager();
    ScriptEngine engine=sem.getEngineByName("js");
    File file=new File("c://1.txt");
    engine.put("f", file);
    try {
      engine.eval("println('path:'+f.getPath())");//无法使用alert方法
    } catch (ScriptException e) {
      e.printStackTrace();
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
* 演示如何在java中如何通过线程来启动一个js方法
*/
public void testScriptInterface() throws ScriptException
{
    ScriptEngineManager sem=new ScriptEngineManager();
    ScriptEngine engine=sem.getEngineByName("js");
    String script="var obj=new Object();obj.run=function(){println('test thread')}";
    engine.eval(script);
    Object obj=engine.get("obj");//获取js中对象
    Invocable inv=(Invocable)engine;
    Runnable r=inv.getInterface(obj,Runnable.class);
    Thread t=new Thread(r);
    t.start();
}

希望本文所述对大家java程序设计有所帮助。

原文链接:http://blog.csdn.net/judyge/article/details/52135803