如果MVel表达式为true,我需要设置属性的值。 任何人都可以帮助我,如何做到这一点。
示例代码如下:
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 :(得分: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。