javascript 在jsp页面中从javascript调用java方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16784322/
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
java method call from javascript in jsp page
提问by Surendra Jnawali
I am new to JSP,
I have method that contains loop and want to display value
我是 JSP 新手,
我有包含循环的方法并想显示值
<%!
private void getDemo() {
for(int i=0; i<10; i++) {
setName("Val: " + i); %>
<script language="javascript">
var name = "<%= getName()%>";
console.log(name);
</script>
<%!}
}%>
<%!
String name;
private void setName(String name) {
this.name = name;
}
private String getName() {
return this.name;
}
%>
Call
称呼
<script type = "text/javascript">
window.onload=function(){<%getDemo();%>};
</script>
Problem
But when I see the console, It only display Val: 9
How can I achieve all value? I need Script code inside Java Code. Is it Possible?
问题
但是当我看到控制台时,它只显示Val: 9
如何实现所有值?我需要 Java 代码中的脚本代码。是否可以?
回答by PSR
Your Javascript runs client side in the browser so will not normally interact with your Java code running server side
您的 Javascript 在浏览器中运行客户端,因此通常不会与运行服务器端的 Java 代码交互