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


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


SimpleScriptContext類的setBindings()方法用於為給定範圍設置屬性的Bindings,其中Bindings和scope作為參數傳遞給該方法。如果範圍是ENGINE_SCOPE,則給定的綁定將替換engineScope字段。如果範圍是GLOBAL_SCOPE,則給定的Bindings將替換globalScope字段。

用法:

public void setBindings(Bindings bindings,
                        int scope)

參數:此方法接受two-parameters:


  • bindings這是要設置和綁定的屬性的綁定
  • scope這是設置屬性的範圍。

返回值:此方法不返回任何內容。

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

  • NullPointerException :如果scope的值為ENGINE_SCOPE並且指定的Bindings為null。
  • IllegalArgumentException:如果範圍無效。

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

// Java program to demonstrate 
// SimpleScriptContext.setBindings() method 
  
import javax.script.ScriptContext; 
import javax.script.SimpleBindings; 
import javax.script.SimpleScriptContext; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create SimpleScriptContext object 
        SimpleScriptContext simple 
            = new SimpleScriptContext(); 
  
        // create Bindings 
        SimpleBindings bindings 
            = new SimpleBindings(); 
  
        // add some key-value to bindings 
        bindings.put("name1", "Value1"); 
  
        // add bindings to SimpleScriptContext using 
        // setBindings() Method 
        simple.setBindings( 
            bindings, 
            ScriptContext.ENGINE_SCOPE); 
  
        // print 
        System.out.println( 
            "name1:"
            + simple.getAttribute("name1")); 
    } 
}
輸出:
name1:Value1

程序2:

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

參考:https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleScriptContext.html#setBindings(javax.script.Bindings,int)



相關用法


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