java 实现 js 方法调用

package com.utils;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * @author wangbingchen
 * java 实现 js 方法调用
 * @since 2022-05-12
 **/
public class ScriptUtil {


    private static ScriptEngine js = new ScriptEngineManager().getEngineByExtension("js");


    public static String escape(String text) {
        return eval("escape('"+text+"')");
    }

    private static String eval(String text) {
        try {
            Object eval = js.eval(text);
            return eval.toString();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return null;
    }

}

posted @ 2022-05-13 13:43  不一样的爪哇  阅读(843)  评论(0编辑  收藏  举报