ognl 之运算符-string 字符串 计算 表达式

ognl 之运算符--string 字符串 计算 表达式

OGNL 作为struts2的一个表达式,这里只介绍它的运算符

     用途,一个string的字符串 String a=“1+1”,如何计算出 a=2 呢,很多开源jar包都可以做到这一点,自己写个代码也可以实现,这里,用ognl运算符来实现,看代码:

 

package test;

import ognl.Ognl;
import ognl.OgnlException;

public class COgnlTest<Variable> {
 public static void main(String[] args) throws ScriptException, OgnlException{
  //这样是不行的
//  String a="1+1";
//  System.out.println(Long.valueOf(a));
  //这样也是不行的
  Object whoExp = Ognl.parseExpression("1+1");
  System.out.println(whoExp);

   String dep=null;
   Object output = Ognl.getValue("1+1", dep);
//   String a="1l";
//   String b="1l";
//   Object output = Ognl.getValue("#a==#b", dep);
   System.out.println("output:"+output);

 }
 
}