简体   繁体   English

从 Javascript 调用 Java 方法的选项

[英]Options to call Java method from Javascript

What are the various options to call a "specific" Java method from Javascript and have callback, etc?从 Javascript 调用“特定”Java 方法并具有回调等的各种选项是什么?

Eg One i know is using DWR..But wanted to know if there are any other libraries that can be used OR if this can be done by AJAX itself..例如,我知道一个正在使用 DWR ..但想知道是否有任何其他库可以使用,或者这是否可以由 AJAX 本身完成..

What is the recommended approach?推荐的方法是什么?

Put simply, with the possible exception of a Java applet running in the same page context as the JavaScript, there is no way for JavaScript to directly call a Java method, or vice-versa. Put simply, with the possible exception of a Java applet running in the same page context as the JavaScript, there is no way for JavaScript to directly call a Java method, or vice-versa. DWR isn't calling any Java methods directly from JavaScript, it's sending an AJAX request to the server and using its own intermediate layer to instrument the request and response processing such that it looks as if it is calling Java methods. DWR isn't calling any Java methods directly from JavaScript, it's sending an AJAX request to the server and using its own intermediate layer to instrument the request and response processing such that it looks as if it is calling Java methods.

But anyways, the typical approach for getting JavaScript code in the client and Java code on the server to talk to one another is to define an API on the server that is exposed to the client.但无论如何,在客户端获取 JavaScript 代码和在服务器上获取 Java 代码以相互交谈的典型方法是在服务器上定义一个暴露给客户端的 API。 The client can then make requests (using AJAX or whatever other mechanism you prefer) using the exposed API.然后,客户端可以使用公开的 API 发出请求(使用 AJAX 或您喜欢的任何其他机制)。 The client isn't calling any Java methods directly, instead it's sending a request to a given URL on the server, which gets mapped back to some bit of Java code.客户端没有直接调用任何 Java 方法,而是向服务器上的给定 URL 发送请求,该请求被映射回 ZD52387880E1EA22817A972D37592 代码的某个位。 Most popular JavaScript frameworks provide convenience methods for simplifying AJAX request handling and callbacks.最流行的 JavaScript 框架提供了用于简化 AJAX 请求处理和回调的便捷方法。

If for some reason you want functionality equivalent to being able to call an arbitrary method in an arbitrary class and have the result returned to you, then you could define an API method that takes as parameters a class name and a method name and then uses reflection to perform the method invocation.如果出于某种原因,您希望功能等同于能够在任意 class 中调用任意方法并将结果返回给您,那么您可以定义一个 API 方法,该方法将 class 名称作为参数,然后使用反射名称和反射方法名称执行方法调用。 This would probably be very close to what the server-side portion of DWR is doing.这可能与 DWR 的服务器端部分所做的非常接近。

I know you can do this when using Mozilla Rhino, but I'm guessing that you're talking about a browser run-time environment, so maybe this response isn't very helpful.我知道你可以在使用 Mozilla Rhino 时做到这一点,但我猜你说的是浏览器运行时环境,所以这个响应可能不是很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM