一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java編程Nashorn實例代碼

Java編程Nashorn實例代碼

2021-03-27 13:22CrazeBean Java教程

這篇文章主要介紹了Java編程Nashorn實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下

本文研究的主要是Java編程Nashorn的相關內容,具體如下。

Nashorn是什么

Nashorn,發音“nass-horn”,是德國二戰時一個坦克的命名,同時也是java8新一代的javascript引擎--替代老舊,緩慢的Rhino,符合 ECMAScript-262 5.1 版語言規范。你可能想javascript是運行在web瀏覽器,提供對html各種dom操作,但是Nashorn不支持瀏覽器DOM的對象。這個需要注意的一個點。

之前學習Java8的時候恰好寫了個簡單的例子,所以放在這里記錄一下。

文件目錄:

  • StringFunction.java,字符串功能類
  • StringNashorn.java,封裝腳本引擎
  • NashornTest.java,測試引擎,引擎調用

StringFunction.java源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class StringFunction {
   
  /**
   * 字符串截取
   */
  public String sub(String str, int start, int end) {
    return str.substring(start, end);
  }
   
  /**
   * 字符串拼接
   */
  public String append(String... strs) {
    StringBuilder result = new StringBuilder(strs[0]);
    Stream.of(strs).skip(1).forEach(str -> result.append(str));
    return result.toString();
  }
}

StringNashorn.java源碼:

?
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
public class StringNashorn {
   
  /**
   * Nashorn腳本引擎
   */
  private ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn");
   
  /**
   * 執行腳本
   */
  public Object execute(String script) {
    ScriptContext scriptContext = new SimpleScriptContext();
    // 定義一個名為stringfunction的函數,這個函數實際對應著一個StringFunction對象
    scriptContext.setAttribute("stringfunction", new StringFunction(), 100);
    nashorn.setContext(scriptContext);
     
    Object result = null;
    try {
      result = nashorn.eval(script);
    } catch (ScriptException e) {
      e.printStackTrace();
    }
     
    return result;
  }
}

NashornTest.java源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class NashornTest {
   
  public static void main(String[] args) {
    String substring = "stringfunction.sub(\"abcdefghijk\", 1, 4);";
    String append = "stringfunction.append(\"abc\", \"def\");";
     
    StringNashorn nashorn = new StringNashorn();
    Object subResult = nashorn.execute(substring);
    Object appendResult = nashorn.execute(append);
    System.out.println(subResult.toString());
    System.out.println(appendResult.toString());
  }
}

運行main方法,運行結果:

bcd
abcdef

這里如果NashornTest.java改寫如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class NashornTest {
   
  public static void main(String[] args) {
    // 腳本內用對象接收結果并打印
    String substring = "var s1 = stringfunction.sub(\"abcdefghijk\", 1, 4);"
        + " print(s1);";
    String append = "var s2 = stringfunction.append(\"abc\", \"def\");"
        + " print(s2);";
     
    StringNashorn nashorn = new StringNashorn();
    // 這里execute不再返回對象,因為在腳本里面已經有對象接收sub和append的執行結果。
    nashorn.execute(substring);
    nashorn.execute(append);
  }
}

同樣也會輸出相同的結果。

總結

以上就是本文關于Java編程Nashorn實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://blog.csdn.net/u010429286/article/details/78951707

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚欧美色 | 五月婷婷丁香在线视频 | 精品一区二区三区色花堂 | tube62hdxxxx日本| a级精品九九九大片免费看 a级动漫 | 精品一区二区三区在线视频观看 | 国产午夜亚洲精品一区网站 | 国产日产欧产精品精品软件 | 男人操男人 | 日本免费三区 | 日本玖玖视频 | 隔壁老王国产在线精品 | 4虎影院永久地址www | 人阁色第四影院在线观看 | 特黄特色大片免费视频大全 | 暖暖免费观看高清在线 | 国产成人综合网 | 性绞姿始动作动态图 | 青苹果乐园影院免费观看完整版 | 999久久久免费精品国产牛牛 | 三星w999 | 国内精品久久久久久不卡影院 | 国产日韩欧美成人 | 7777色鬼xxxx欧美色夫 | 91东航翘臀女神在线播放 | 99re这里只有精品在线观看 | 千金肉奴隶在线观看 | 青草青青在线 | 1024免费观看完整版在线播放 | a一级黄| 欧美bbb人妖| 色哟哟精品 | 日韩一级欧美一级一级国产 | 好大好硬好深好爽gif图 | 日本一区二区三区四区无限 | 午夜精品久久久久 | 色播开心网 | 国内精品久久久久久不卡影院 | 成人免费观看www视频 | 修修视频在线观看 | 午夜免费啪视频观看视频 |