• 用ScriptEngine在java中和javascript交互的例子(JDK6新特性)


    package demo7;
    
    import java.util.Arrays;
    import java.util.List;
    
    import javax.script.Invocable;
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    
    public class ScriptEngineTest {
    
        public static void main(String[] args) throws Exception {
            ScriptEngineManager sem = new ScriptEngineManager();
            ScriptEngine engine = sem.getEngineByName("javascript");
    
            // 1
            engine.put("msg", "just a test");
            String str = "msg += '!!!';var user = {name:'tom',age:23,hobbies:['football','basketball']}; var name = user.name; var hb = user.hobbies[1];";
            engine.eval(str);
    
            String msg = (String) engine.get("msg");
            String name = (String) engine.get("name");
            String hb = (String) engine.get("hb");
            System.out.println(msg);
            System.out.println(name + ":" + hb);
    
            // 2
            engine.eval("function add (a, b) {c = a + b; return c; }");
            Invocable jsInvoke = (Invocable) engine;
    
            Object result1 = jsInvoke.invokeFunction("add", new Object[] { 10, 5 });
            System.out.println(result1);
    
            // 3
            Adder adder = jsInvoke.getInterface(Adder.class);
            int result2 = adder.add(10, 35);
            System.out.println(result2);
    
            // 4
            engine.eval("function run() {print('www.java2s.com');}");
            Invocable invokeEngine = (Invocable) engine;
            Runnable runner = invokeEngine.getInterface(Runnable.class);
            Thread t = new Thread(runner);
            t.start();
            t.join();
    
            // 5
            String jsCode = "importPackage(java.util);var list2 = Arrays.asList(['A', 'B', 'C']); ";
            engine.eval(jsCode);
            List<String> list2 = (List<String>) engine.get("list2");
            for (String val : list2) {
                System.out.println(val);
            }
            
        }
    }
    
    interface Adder {
        int add(int a, int b);
    }
  • 相关阅读:
    title中的小图标
    JS
    浏览器的差距
    浏览器的前缀
    布局
    。。。 付解决浏览器兼容问题的两种方法
    form标签
    position讲解
    标题文档流与浮动
    css层叠问题
  • 原文地址:https://www.cnblogs.com/fm168/p/3396651.html
Copyright © 2020-2023  润新知