当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。