📜  Java中的 SimpleScriptContext getBindings() 方法及示例

📅  最后修改于: 2022-05-13 01:55:35.381000             🧑  作者: Mango

Java中的 SimpleScriptContext getBindings() 方法及示例

SimpleScriptContext 类的getBindings()方法用于返回与此 ScriptContext 中的给定范围关联的绑定,其中范围作为参数传递。

句法:

Bindings getBindings(int scope)

参数:此方法接受单个参数范围,即此 ScriptContext 中的范围。

返回值:该方法返回关联的Bindings,如果没有设置则返回null。

异常:此方法抛出以下异常:

  • IllegalArgumentException :如果没有为此类型的 ScriptContext 中的指定范围值定义绑定。

下面的程序说明了 SimpleScriptContext.getBindings() 方法:
方案一:

// 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

方案二:

// 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)