registerFunction方法使用

本文介绍如何在 Spring Expression Language (SpEL) 中注册自定义函数并使用它来反转字符串。通过创建 ExpressionParser 和 StandardEvaluationContext 实例,并注册 StringUtils 的 reverseString 方法,实现了对输入字符串 'hello' 的反转。
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.registerFunction("reverseString", StringUtils.class.getDeclaredMethod("reverseString", new Class[] { String.class }));
String helloWorldReversed = parser.parseExpression("#reverseString('hello')").getValue(context, String.class);
<!DOCTYPE html> <html> <head> <title>城市大脑demo示例</title> <script src="./js/vue2.js"></script> <script src="./js/gpt.umd.js"></script> <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script> <style> #app { display: flex; justify-content: space-between; flex-wrap: wrap; button { width: 48%; height: 44px; line-height: 44px; font-size: 20px; margin-bottom: 10px; cursor: pointer; } } </style> </head> <body> <div id="app"> <button @click="sendMessage('test', { message: '测试消息' })"> 发送消息 </button> <button @click="sendMessage('getUseInfo')">获取用户信息</button> <button @click="sendMessage('logout')">退出登录</button> <button @click="sendMessage('goBack')">返回</button> <button @click="updateChat('updateChat')">同步对话</button> </div> <script> var vConsole = new VConsole(); // 创建一个Vue实例 new Vue({ el: "#app", data: { cityBrainSdk: null, appId: "f182e97725", targetOrigin: "https://ai.citybrain.hangzhou.gov.cn", }, mounted() { window.addEventListener("message", function (event) { console.log(event,'子页面接收到的'); alert("Parameter from parent:" + JSON.stringify(event.data)); }); this.cityBrainSdk = new CityBrain({ appId: this.appId, targetOrigin: this.targetOrigin, }); console.log("城市大脑实例--", this.cityBrainSdk); // 接收返回的数据信息-与发送的type值一样 this.cityBrainSdk.registerFunction("getUseInfo", (data) => { console.log("registerFunction:getUseInfo--", data); }); }, methods: { sendMessage(type, data = {}) { console.log("sendMessage:type--", type); this.cityBrainSdk.sendMessage(type, data); }, updateChat(type) { this.cityBrainSdk.sendMessage("updateChat", { appId: this.appId, question: "问题", answer: "答案", }); }, }, }); </script> </body> </html> 这里的registerFunction方法是undifind,结合刚才的js代码,看一下,这个方法为什么是undifind
09-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值