【问题标题】:Mvel how to set valueMvel如何设置值
【发布时间】:2013-07-03 23:15:53
【问题描述】:

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

【问题讨论】:

    标签: java mvel


    【解决方案1】:

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

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

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

    输出 - 20

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 2021-02-10
      • 2012-07-26
      • 2016-02-06
      • 1970-01-01
      相关资源
      最近更新 更多