java beanshell,Java类字段,Beanshell中的对象使用

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 map = new HashMap();

static List list = new ArrayList();

static Map integerMap = new HashMap();

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)list){

System.out.println(str);

}

}

}

I want to perform all the operation which is available for collection in java to same object in beanshell.

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.

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.

解决方案

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

and get:

vars.getObject("stringList");

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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值