代码之家  ›  专栏  ›  技术社区  ›  Joe Ijam

JEXL计划有“;”例外

  •  0
  • Joe Ijam  · 技术社区  · 14 年前

            String strDuration = "4560";
            long lDuration = Long.parseLong(strDuration);
            String theExpression = "" +
                    "if(lDuration > 500)" +
                    "   return true;" +
                    "else" +
                    "   return false;";
    
            Expression e =  jexl.createExpression( theExpression );
            JexlContext context = new MapContext();
            context.set("lDuration", lDuration);
            Boolean result =(Boolean) e.evaluate(context);
            System.out.println("The answer : " + result);
    

    例外情况:

    谁能帮我显示我想要的输出(布尔值)?

    提前谢谢。

    1 回复  |  直到 14 年前
        1
  •  1
  •   Syntax    14 年前

    干得好:

      public static void main(String[] args) {
        String strDuration = "4560";
        long lDuration = Long.parseLong(strDuration);
        String theExpression = "(lDuration > 500) ? true : false;";
        JexlEngine jexl = new JexlEngine();
        Expression e = jexl.createExpression(theExpression);
        JexlContext context = new MapContext();
        context.set("lDuration", lDuration);
        Boolean result = (Boolean) e.evaluate(context);
        System.out.println("The answer : " + result);
      }
    

    编辑: 要清楚的是,问题是您使用了return语句,JEXL似乎不支持它。