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)
相关用法
- Java SimpleScriptContext removeAttribute()用法及代码示例
- Java SimpleScriptContext getAttributesScope()用法及代码示例
- Java SimpleScriptContext getAttribute()用法及代码示例
- Java SimpleScriptContext setAttribute()用法及代码示例
- Java SimpleScriptContext getBindings()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 SimpleScriptContext setBindings() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。