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

 }
 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值