Javascript 如何将我的 Java 包导入到 js 文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3768070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:04:00  来源:igfitidea点击:

How can I import my Java packages into a js file

javascript

提问by John

I was wondering how to import my java packages into a js file and then call class methods on created object.

我想知道如何将我的 java 包导入到 js 文件中,然后在创建的对象上调用类方法。

The reason I need to import is I wish to get data from a connection to my database, regarding dates and I have a class that lets me do so. So I need to import the Package, then create a class object, then call methods on my object.

我需要导入的原因是我希望从与我的数据库的连接中获取数据,关于日期,并且我有一个允许我这样做的类。所以我需要导入包,然后创建一个类对象,然后在我的对象上调用方法。

I would appreciate your help, Thank you, John

我会很感激你的帮助,谢谢你,约翰

回答by Scott Offen

The questions does not specify that this is a browser-based question. Java ships with Rhino since version 6. So, on the off chance that someone wants to know how to do this - e.g., using JavaScript as a scripting language for 3rd party tools like soapUIor PTC Integrity- it's incredibly simple.

这些问题并未指明这是一个基于浏览器的问题。Java 从版本 6 开始随 Rhino 一起提供。因此,如果有人想知道如何执行此操作(例如,使用 JavaScript 作为诸如soapUIPTC Integrity 之类的第三方工具的脚本语言),这非常简单。

importPackage(java.io);

Check out Wikipedia:Rhino (JavaScript engine)as a good starting point.

查看Wikipedia:Rhino(JavaScript 引擎)作为一个很好的起点。

回答by Joshua Partogi

You can use JSP and use Java basic types as parameter for your javascript.

您可以使用 JSP 并使用 Java 基本类型作为 javascript 的参数。

Import your Java classes in JSP, e.g:

在 JSP 中导入您的 Java 类,例如:

<%@page import="com.acme.MyClass"%>

<%@page import="com.acme.MyClass"%>

Then in your JS which also in the JSP:

然后在你的 JS 中,它也在 JSP 中:

<script type="text/javascript">
var f = function(param){
 console.log(param);
}

f('<%= MyClass.foo().toString() %>');
</script>

Would that be something you're looking at?

那会是你正在看的东西吗?

Hope that helps.

希望有帮助。

回答by Zango

I think solution for you problem is DWRtechnology. As I guessed you think that it is too easy to invoke Java methods(Server side) from JavaScript(Client side). DWR is exactly what you are looking for :)

我认为解决您问题的方法是DWR技术。正如我猜想的那样,您认为从 JavaScript(客户端)调用 Java 方法(服务器端)太容易了。DWR 正是您要找的 :)

回答by Randy the Dev

You'll have to store the Java as a Java server page on your server to do the connection, and feed javascript the database data via ajax, because javascript is incapable of connecting to any protocols other than http, file and ftp.

您必须将 Java 作为 Java 服务器页面存储在您的服务器上才能进行连接,并通过 ajax 向 javascript 提供数据库数据,因为 javascript 无法连接到除 http、file 和 ftp 之外的任何协议。

回答by mkoistinen

Actually you CAN do this. You can create a Java Applet with public methods. Once you add this to your page you can access these methods via JavaScript. I'll see if I can dig up a reference for you.

其实你可以这样做。您可以使用公共方法创建 Java Applet。将其添加到页面后,您可以通过 JavaScript 访问这些方法。我看看能不能给你找个参考。

Here's one link!

这是一个链接

I've done this myself, years ago. It works well. I don't know if I would do it again for the reason you specify, but it is possible.

几年前我自己做过。它运作良好。我不知道我是否会因为你指定的原因再做一次,但这是可能的。

Also in addition to this method and the many other fine suggestions, have a look at GWT. http://code.google.com/webtoolkit/overview.html

另外除了这个方法和许多其他好的建议,看看 GWT。http://code.google.com/webtoolkit/overview.html

回答by Jaydeep Patel

Calling java methods from javascript is not possible.

无法从 javascript 调用 java 方法。

JSP file:

JSP文件:

function doSomething {
   <% SomeClass.someMethod(); %>
}

<%!
class SomeClass {
  public static void someMethod() {
    // 
  }
}
%>

someMethod will not run on invocation of doSomething but it will run when jsp generates response.

someMethod 不会在调用 doSomething 时运行,但会在 jsp 生成响应时运行。