Java类字段,Beanshell中的对象使用 [英] Java class fields, object use in Beanshell
问题描述
我正在将Java与JSF和Beanshell脚本一起使用.我想在beanshell中使用Java类的字段和对象.我已尽力从Google获得帮助,但找不到任何有用的信息. 例如
I am using Java with JSF and Beanshell script. I want to use fields and object of java class in beanshell. I have tried my best to get help from google but couldn't find any helpful information. For example
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import bsh.EvalError;
import bsh.Interpreter;
public class C {
static Map<String,Object> map = new HashMap<String,Object>();
static List<String> list = new ArrayList<String>();
static Map<String,Integer> integerMap = new HashMap<String,Integer>();
public static void main(String[] arg) throws EvalError{
list.add("Hello");
list.add("World");
Interpreter i = new Interpreter(); // Construct an interpreter
map.put("stringList", list);//in java
i.eval("map.put(\"stringList\", list)");// gives error
List list = (List) map.get("stringList");
for(String str:(List<String>)list){
System.out.println(str);
}
}
}
我想执行所有可用于Java中收集到beanshell中同一对象的操作.
I want to perform all the operation which is available for collection in java to same object in beanshell.
Jmeter 提供了这样的功能,用户可以在beanshell中更新变量并基于在链接上给出的详细信息,看来Jmeter正在使用字符串映射,我想做同样的事情,但它是针对对象的.
Jmeter provide such facilities where user can update the variable in beanshell and based on the details given on link, it seems Jmeter is using string map and I want to do same things but it is with objects.
如果您可以使用Java中的任何技术或框架满足Java,beanshell,JSF或Java中其他可用选项的要求,我将不胜感激.
I would appreciate your inputs, if any technology in or framework which can be used to achieve my requirement would be good either java, beanshell, JSF or other available option in java.
推荐答案
在JMeter的Beanshell或更好的JSR223 Sampler(Java语言)中,您可以将对象放在JMeter变量中,如下所示:
In JMeter's Beanshell or better JSR223 Sampler (Java language) you can put objects in JMeter variables as put:
JMeterVariables vars = JMeterContextService.getContext().getVariables();
vars.putObject("stringList", stringList);
并获得:
vars.getObject("stringList");
在Java一般情况下,您需要使用set
方法将变量添加到Beanshell Interpreter
:
In Java general case you need to add variable to Beanshell Interpreter
with set
method:
list.add("Hello");
list.add("World");
Interpreter i = new Interpreter(); // Construct an interpreter
map.put("stringList", list);//in java
try {
i.set("map", map);
i.set("list", list);
System.out.println(i.eval("map.put(\"stringList\", list)"));
} catch (EvalError e1) {
e1.printStackTrace();
}
这篇关于Java类字段,Beanshell中的对象使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!