用户定义一个字符串 ${isApprove} == 'Y' 我存到数据库了当我读出这个串赋值给一个变量String str="${isApprove} == 'Y' ";后
我如果得到isApprove 的值可能是'Y'或'N'   那么我如何能知道str的结果是true或false如果是1==0 数学表达式用eval.jar可以判断出来,但找了半天没发现有字符的表达式判断啊求助高手

解决方案 »

  1.   

    也可以啊,你先做变量替换嘛,也就是要把${isApprove}的值设置进去,然后才能进行表达式计算啊。
      

  2.   

                     String strVal1="-100 < 100 && 200>100  ";
    Expression exp1 = new Expression(strVal1);
    System.out.println(exp1.eval()); 
    String strVal="'Y'=='Y'";
    Expression exp = new Expression(strVal);
    System.out.println(exp.eval()); System.out.println(exp1.eval()); 
    可以得到结果为1 就是true
    但System.out.println(exp.eval()); 
    这句是抛异常的
      

  3.   

                     String strVal1="-100 < 100 && 200>100  ";
    Expression exp1 = new Expression(strVal1);
    System.out.println(exp1.eval()); 
    String strVal="Y==Y";
    try{
    Expression exp = new Expression(strVal);
    System.out.println(exp.eval()); 
    }catch (Exception e){
    System.out.println(e);
    }java.lang.RuntimeException: no value for variable "Y"如果不加单引号 那么他把Y当变量 不认识啊
    加了引号报这个错误
    java.lang.RuntimeException: operand expected but ''' found 
      

  4.   

    String strVal1="-100 < 100 && 200>100  ";
    Expression exp1 = new Expression(strVal1);
    System.out.println(exp1.eval()); 
    String strVal="Y==Y";
    Map<String, String> variables = new HashMap<String, String>();
    variables.put("Y", new String("Y"));
    try{
    Expression exp = new Expression(strVal);
    System.out.println(exp.eval(variables)); 
    }catch (Exception e){
    System.out.println(e);
    }
    换成这样也不行 eval里的variables 只能是Map<String,BigDecimal> 不能是Map<String, String>