0

如果 MVel 表达式为真,我需要设置属性的值。任何人都可以帮助我,如何做到这一点。

示例代码如下:

      LineItem lineItem = new LineItem();

      Address address = new Address();
        address.setAddress1("ABC");
        address.setAddress2("PA");

      lineItem.setShipFromAddress(address);

    ParserContext parserContext = ParserContext.create();
    parserContext.stronglyTyped().withInput("lineItem",LineItem.class)
          .withInput("shipFromAddress", Address.class);

        Object compiledWithSet = MVEL.compileExpression("( shipFromAddress.address1 contains 'ABC' || shipFromAddress.address1 contains 'ABC DEF' ) && (shipFromAddress.address2 contains 'PA') ? setShipFromLocation('PA1') : ",parserContext);
        MVEL.executeExpression(compiledWithSet, lineItem);
4

1 回答 1

0

您的问题有一个解决方案,但您能否详细说明您的用例。这是小样本答案,希望这可以帮助您入门。

public class MyMaths {

    int a;

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }
}

public static void main(String[] args) {

        Map map = new HashMap();
        MyMaths mayMaths = new MyMaths();
        map.put("obj", mayMaths);
        map.put("name", "Ankur");

        String expression1 = "obj.a = ( name == 'Ankur') ? 20 : 25";

        Serializable compiled1 = MVEL.compileExpression(expression1);

        MVEL.executeExpression(compiled1, map);

        System.out.println(mayMaths.getA());

    }

所以在这里我实际上是将值分配给a类的变量“”MyMaths

输出 - 20

现在将值从“Ankur”更改为“XYZ”,输出将为 25。

于 2014-06-26T03:42:00.397 回答