java判断js表达式,如:“7>2“是否正确

方式一:jdk原生api

方式二:hutool工具类

方式一:

maven


 
            javax.servlet
            javax.servlet-api
        
        
            javax.servlet
            jstl
        

代码


  private static boolean isRight(String el){
        // String el = "7.3 >= 500";
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        boolean eval = false;
        try {
            eval = (boolean) engine.eval(el);
            // System.out.println(eval);
        } catch (Exception e) {
            e.printStackTrace();
            return eval;
        }
        return eval;
    }
方式二:

 maven

     
            cn.hutool
            hutool-all
        

方法:

ScriptUtil.eval("7>2");

你可能感兴趣的:(java)