2018年4月3日

[JAVA]用JEXL處理算式

/**

* @author Querida
http://solnone.blogspot.tw/2010/06/apache-commons-jexl.html
*/

package testQ;



import org.apache.commons.jexl3.JexlContext;

import org.apache.commons.jexl3.JexlEngine;

import org.apache.commons.jexl3.JexlExpression;

import org.apache.commons.jexl3.MapContext;

import org.apache.commons.jexl3.internal.Engine;



public class TestJEXL {


public static void main(String[] args) {

// Create or retrieve a JexlEngine

JexlEngine jexl = new Engine();

// Create an expression object

String fomula = "quanty * price * 100";

JexlExpression jexlExpression = jexl.createExpression(fomula);



// Create a context and add data

JexlContext jexlContext = new MapContext();

jexlContext.set("quanty", 2);

jexlContext.set("price", 3);

// Now evaluate the expression, getting the result

Object o = jexlExpression.evaluate(jexlContext);

System.out.println(o);

}

}

沒有留言:

張貼留言