栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java调用js方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java调用js方法

js函数保存在String字符串中
package com.netease.qiyu.test.datastageservice.service.impl;

import com.netease.qiyu.test.datastageservice.service.KefuSipAccountRegisterService;
import org.springframework.stereotype.Service;

import javax.script.Invocable;
import javax.script.scriptEngine;
import javax.script.scriptEngineManager;
import javax.script.scriptException;
import java.io.IOException;
import java.io.StringReader;


@Service
public class KefuSipAccountRegisterServiceImpl implements KefuSipAccountRegisterService {

    public static void initialToolbarSDK(){

        // js function:getRouteInfo,入参为province
        // 参数不要带var。。不然后面执行方法的时候会报错。。
        String routescript = "function getRouteInfo(province){ n" + "      if (province=='henan') "
                + "         return 'http://127.0.0.1/resweb';n" + "      else  "
                + "         return '未找到对应的省份信息,province='+province;n" + "}";
        // 脚本的执行结果
        String scriptResult = "";
        // 1.得到脚本引擎
        scriptEngine engine = new scriptEngineManager().getEngineByName("nashorn");
        try {
            // 2.引擎读取 脚本字符串
            engine.eval(new StringReader(routescript));
            // 如果js存在文件里,举例
            // Resource aesJs = new ClassPathResource("js/aes.js");
            // this.engine.eval(new FileReader(aesJs.getFile()));
            // 3.将引擎转换为Invocable,这样才可以掉用js的方法
            Invocable invocable = (Invocable)engine;
            // 4.使用 invocable.invokeFunction掉用js脚本里的方法,第一個参数为方法名,后面的参数为被调用的js方法的入参
            scriptResult = (String)invocable.invokeFunction("getRouteInfo", "henan");
        } catch (scriptException e) {
            e.printStackTrace();
            System.out.println("Error executing script: " + e.getMessage() + " script:[" + routescript + "]");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            System.out.println("Error executing script,为找到需要的方法: " + e.getMessage() + " script:[" + routescript + "]");
        }
        System.out.println(scriptResult.toString());
    }

    public static void main(String args[]){
        initialToolbarSDK();
    }
}
js函数保存在.js文件中
    public static void initialToolbarSDK(){
        String scriptResult = "";
        scriptEngine engine = new scriptEngineManager().getEngineByName("nashorn");
        try {
            //文件路径设置成绝对路径即可
            String jsFile = "service/impl/test.js";
            FileInputStream fileInputStream = new FileInputStream(new File(jsFile));
            Reader scriptReader = new InputStreamReader(fileInputStream, "utf-8");
            engine.eval(scriptReader);
            Invocable invocable = (Invocable)engine;
            scriptResult = (String)invocable.invokeFunction("getRouteInfo", "henan1");
        } catch (scriptException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(scriptResult.toString());
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778111.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号