在 JavaScript 函数中调用 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6710382/
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
Calling Java inside JavaScript Function
提问by
Please tell me if we can call java inside javascript function ?
请告诉我我们是否可以在javascript函数中调用java?
<HTML><HEAD></HEAD><BODY>
<SCRIPT>
function getScreenDimension() {
<% System.out.println("Hiiiiiiiii"); %>
}
</SCRIPT>
<FORM>
<INPUT type="button" value="call Java method direct" onClick = "getScreenDimension()">
</FORM>
</BODY></HTML>
采纳答案by James Montagne
While the answer of "No" is technically correct based on the phrasing of the question. You may want to read up on AJAX. It is a way for javascript to make a request to your backend code (in this case Java).
虽然根据问题的措辞,“否”的答案在技术上是正确的。您可能想阅读有关 AJAX 的内容。这是 javascript 向您的后端代码(在本例中为 Java)发出请求的一种方式。
Javascript is client side, meaning it is run by the user's browser. Java is running on your server. In order for the client side javascript to interact with the backend Java, you need to make a request to the server.
Javascript 是客户端,这意味着它由用户的浏览器运行。Java 正在您的服务器上运行。为了让客户端 javascript 与后端 Java 交互,您需要向服务器发出请求。
回答by Vivin Paliath
My question to you would be, "What are you trying to do, and what do you expect to see?".
我要问你的问题是,“你想做什么,你希望看到什么?”。
You have to realize that there are two different execution contexts. The first is the JSP itself whose code is executed by the JVM on the server-side, and the second is the Javascript that is executed by the browser. So when the code goes to the browser, you'll see:So the System.out.println
will cause Hiiiiiiiii
to be printed to the server logs, but you won't see anything on the browser. In fact, the Javascript code on the browser will look like this:
您必须意识到有两种不同的执行上下文。第一个是JSP本身,其代码由服务器端的JVM执行,第二个是浏览器执行的Javascript。所以当代码进入浏览器时,你会看到:So the System.out.println
will cause Hiiiiiiiii
to print to the server logs,但你不会在浏览器上看到任何东西。实际上,浏览器上的 Javascript 代码如下所示:
function getScreenDimension() {
}
Which is not valid Javascript code.The code in the JSP is run before the Javascript gets run on the browser. So to "run" Java code, you need to make a request to your server either by posting the form or with an AJAX call. This will cause Java code in the appropriate servlet or controller, to run.
这不是有效的 Javascript 代码。JSP 中的代码在 Javascript 在浏览器上运行之前运行。因此,要“运行”Java 代码,您需要通过发布表单或使用 AJAX 调用向您的服务器发出请求。这将导致相应 servlet 或控制器中的 Java 代码运行。
UPDATE
更新
After glancing at your code, it appears that you want to call a Java method directly. This is not possible with your current code. You might want to read up on AJAX. This will point you in the right direction.
看了你的代码后,你似乎想直接调用一个 Java 方法。您当前的代码无法做到这一点。您可能想阅读有关 AJAX 的内容。这将为您指明正确的方向。
回答by Quentin
JSP runs on the server. It generates a document which the server sends to the browser. That is the end of the involvement of JSP in the process. The browser then parses the document and runs any JS.
JSP 在服务器上运行。它生成服务器发送到浏览器的文档。JSP 参与的过程到此结束。然后浏览器解析文档并运行任何 JS。
You caninclude JSP in a script element, it just has to output valid JavaScript.
您可以在脚本元素中包含 JSP,它只需要输出有效的 JavaScript。
You cannothave JSP that runs in response to JavaScript, other then when JavaScript causes the browser to issue a new HTTP request (either setting location.href
, submitting a form, adding an image, or using Ajax, etc)
您不能让 JSP 响应 JavaScript 运行,除非 JavaScript 导致浏览器发出新的 HTTP 请求(设置location.href
、提交表单、添加图像或使用Ajax等)
回答by LovaBill
Yes, you can. Use JSP expressions <%= %>
. Example:
是的你可以。使用 JSP 表达式<%= %>
。例子:
<aui:script use="aui-datepicker">
AUI().use('aui-datepicker', function(A) {
new A.DatePickerSelect({
calendar : {
dates : [ '<%="1/1/1970" %>' ],
}
}).render('#myDatePicker');
});
</aui:script>
回答by Amir Raminfar
I think you have lack of understanding of what is going on here. Anything in middle of <% %>
is executed when the page is first requested. Anything in javascript is executed when the browser calls it. What you have will neverhappen and there is no way to make it happen. However, you can use AJAX to do something likethis but that's a different question.
我认为你对这里发生的事情缺乏了解。<% %>
当第一次请求页面时,会执行中间的任何内容。当浏览器调用它时,javascript 中的任何内容都会被执行。你所拥有的永远不会发生,也没有办法让它发生。但是,你可以使用AJAX做一些像这样,但是这是一个不同的问题。
回答by max3d
You can. In JSF you can use PrimeFaces' component p:remoteCommand, which would contain action method. Call that remoteCommand by its name from JS and the java method will get executed.
你可以。在 JSF 中,您可以使用 PrimeFaces 的组件p:remoteCommand,其中包含操作方法。从 JS 中按名称调用该 remoteCommand,java 方法将被执行。
JSF Page
JSF页面
<p:remoteCommand name='rmt' action="#{bean.doWork()}"/>
In JavaScript
在 JavaScript 中
function callJava {rmt();}
回答by Bernd Elkemann
Yes, you can call Java from Javascript, both if you mean the Java-VM on the server and on the client; i assume you mean the client (the VM in the browser); take a look here:
是的,您可以从 Javascript 调用 Java,无论是指服务器上还是客户端上的 Java-VM;我假设你的意思是客户端(浏览器中的虚拟机);看看这里:
回答by Londeren
Use JSP code
使用 JSP 代码
<%
// Respond to the application with 1) result, 2) update, and 3) updateid values
String result = "blablabla";
out.print(result);
%>