试用commons-jexl

试用commons-jexl
下载地址:
http://commons.apache.org/jexl/

测试代码:
import org.apache.commons.jexl.*;
public class Demo
{
 public static void main(String[] args)  throws Exception
 {
      // Create an expression object
   String jexlExp = “df.format(foo.getTime());”;
   Expression e = ExpressionFactory.createExpression( jexlExp );
   
   // Create a context and add data
   JexlContext jc = JexlHelper.createContext();
   java.text.SimpleDateFormat df=new java.text.SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
   jc.getVars().put(“df”, df);
   jc.getVars().put(“foo”, new java.util.Date() );
   
   // Now evaluate the expression, getting the result
   String o = (String)e.evaluate(jc);
   System.out.println(o);
 }
}

点赞