當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java SimpleScriptContext getBindings()用法及代碼示例


SimpleScriptContext類的getBindings()方法用於返回與此給定範圍相關聯的綁定,該綁定與此ScriptContext中的範圍作為參數傳遞。

用法:

Bindings getBindings(int scope)

參數:此方法接受單個參數範圍,這是此ScriptContext中的範圍。


返回值:此方法返回關聯的Bindings,如果尚未設置,則返回null。

異常:此方法引發以下異常:

  • IllegalArgumentException:如果在此類型的ScriptContext中沒有為指定的作用域值定義任何綁定,請執行以下操作:

以下示例程序旨在說明SimpleScriptContext.getBindings()方法:
程序1:

// Java program to demonstrate 
// SimpleScriptContext.getBindings() method 
  
import javax.script.Bindings; 
import javax.script.ScriptContext; 
import javax.script.SimpleScriptContext; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create SimpleScriptContext object 
        SimpleScriptContext simple 
            = new SimpleScriptContext(); 
  
        // add some attribute 
        simple.setAttribute( 
            "name1", 
            "Value1", 
            ScriptContext.ENGINE_SCOPE); 
        simple.setAttribute( 
            "name2", 
            "Value2", 
            ScriptContext.ENGINE_SCOPE); 
  
        // get bindings using getBindings() 
        Bindings bindings 
            = simple.getBindings( 
                ScriptContext.ENGINE_SCOPE); 
  
        // print 
        System.out.println("Bindings value for name1:"
                           + bindings.get("name1")); 
        System.out.println("Bindings value for name2:"
                           + bindings.get("name2")); 
    } 
}
輸出:
Bindings value for name1:Value1
Bindings value for name2:Value2

程序2:

// Java program to demonstrate 
// SimpleScriptContext.getBindings() method 
  
import javax.script.Bindings; 
import javax.script.ScriptContext; 
import javax.script.SimpleScriptContext; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create SimpleScriptContext object 
        SimpleScriptContext simple 
            = new SimpleScriptContext(); 
  
        // add some attribute 
        simple.setAttribute( 
            "Team1", 
            "India", 
            ScriptContext.ENGINE_SCOPE); 
        simple.setAttribute( 
            "Team2", 
            "Japan", 
            ScriptContext.ENGINE_SCOPE); 
        simple.setAttribute( 
            "Team3", 
            "Nepal", 
            ScriptContext.ENGINE_SCOPE); 
  
        // get bindings using getBindings() 
        Bindings bindings 
            = simple.getBindings( 
                ScriptContext.ENGINE_SCOPE); 
  
        // print 
        System.out.println("Bindings value for Team1:"
                           + bindings.get("Team1")); 
        System.out.println("Bindings value for Team2:"
                           + bindings.get("Team2")); 
        System.out.println("Bindings value for Team3:"
                           + bindings.get("Team3")); 
    } 
}
輸出:
Bindings value for Team1:India
Bindings value for Team2:Japan
Bindings value for Team3:Nepal

參考:https://docs.oracle.com/javase/10/docs/api/javax/script/ScriptContext.html#getBindings(int)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 SimpleScriptContext getBindings() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。