2013-07-03 22 views
0

如果MVel表達式爲true,則需要設置屬性的值。 任何人都可以幫助我,怎麼做。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); 

回答

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

    } 

所以我在這裏居然分配值類的變量「aMyMaths

輸出 - 20

現在改變從「ANKUR」到「XYZ」,則輸出將是25.