如何将参数传递给在MVEL表达式内编写的函数? [英] How to pass arguments to a function written inside an MVEL expression?

查看:1126
本文介绍了如何将参数传递给在MVEL表达式内编写的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有两个方法的JAVA类.第一个是主要方法,第二个是method1().

I have a JAVA class that has two methods. The first one is the main method and the second one is method1().

让我们说以下是课程:

public class SomeClass() {
  public static void main(String[] args) {
    SomeClass myObj = new SomeClass();
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("obj", myObj);
    MVEL.eval("System.out.println(\"I am inside main method\");obj.method1();",map);
  }
  public static void method1(List<String> listOfStrings){
    System.out.println("I am inside method 1");
  }
}

现在您可以在表达式中看到,要调用method1,我需要传递一个列表作为参数.怎么做?表达式中需要哪些更改?如果我想在程序中传递动态参数怎么办?

Now as you can see in the expression, to call method1, I need to pass a list as arguments. How to do that? What changes are required in the expression? What if I want to pass dynamic arguments in my program?

推荐答案

您可以创建List或将其作为其他参数来自其他来源.

You can create a List or have it coming from some other source as an argument.

您只需要注意的是将map对象放入其中, 由MVEL用于评估.

Only thing you need to take care is to put inside the map object, which used by MVEL for evaluation.

需要通过提及的名单-> obj.method1(myList);

Need to pass list as mentioned -> obj.method1(myList);

下面的工作代码

public class SomeClass {
    public static void main(String[] args) {
        SomeClass myObj = new SomeClass();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("obj", myObj);

        List<String> listOfStrings = new ArrayList<String>();
        listOfStrings.add("my ");
        listOfStrings.add("List ");
        listOfStrings.add("is printing");

        map.put("obj", myObj);
        map.put("myList", listOfStrings);

        MVEL.eval("System.out.println(\"I am inside main method\");obj.method1(myList);",map);
    }

    public static void method1(List<String> listOfStrings) {
        System.out.println("I am inside method 1");
        for (String s : listOfStrings) {
            System.out.print(s);
        }
    }
}

输出

I am inside main method
I am inside method 1
my List is printing

这篇关于如何将参数传递给在MVEL表达式内编写的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆