在 onclick 事件期间从速度模板调用 java 函数
To invoke java function from velocity template during onclick event
Web 应用程序使用 struts2 和速度
1)
速度文件有一个 link,单击 link 时,我希望调用 java 函数。
productform.vm
<a href="javascript:void(0);" onclick="*************" id="acnt_link"> Add Acnt </a>
当单击速度文件中的 link 时,我需要调用 java 函数
abc.java
public void printabc(){
syso("Java function called when link from velocity temp is clicked");
}
请指教如何做到这一点
2)
我可以在单击来自速度的 link 时调用 java 脚本函数,但是从 java 脚本函数我如何调用 java 函数(例如 abc.printabc())
请指教
你可以看看DWR which gives you methods from java as javascript function. Directly taken from their Tutorial:
public class Remote {
public String getData(int index) { ... }
}
以及相应的 html 片段:
<head>
<!-- other stuff -->
<script type="text/javascript" src="[WEBAPP]/dwr/interface/Remote.js"> </script>
<script type="text/javascript" src="[WEBAPP]/dwr/engine.js"> </script>
<script>
function myRemoteMethod() {
Remote.getData(42, {
callback: function(str) {
alert(str);
}});
}
</script>
</head>
<body>
<!-- ... -->
<a href="javascript:void(0);" onclick="myRemoteMethod()">Add Acnt</a>
<!-- ... -->
</body>
确保将 DWR 集成到 struts,他们也有相关的教程:DWR Struts Tutorial。
另一个解决方案是通过 ajax 请求调用 java 方法:
function myRemoteMethod() {
$.get('/link/to/method/')
.done(function (data) {
alert('Return message of server was:' + data);
});
}
这是一个 jQuery 示例,因此请确保您的 html 代码的 <head>
中包含该库。在 html 代码中调用此方法,然后通过 ajax.
"call" java 方法
<a href="javascript:void(0);" onclick="myRemoteMethod()">Add Acnt</a>
Web 应用程序使用 struts2 和速度
1)
速度文件有一个 link,单击 link 时,我希望调用 java 函数。
productform.vm
<a href="javascript:void(0);" onclick="*************" id="acnt_link"> Add Acnt </a>
当单击速度文件中的 link 时,我需要调用 java 函数
abc.java
public void printabc(){
syso("Java function called when link from velocity temp is clicked");
}
请指教如何做到这一点
2)
我可以在单击来自速度的 link 时调用 java 脚本函数,但是从 java 脚本函数我如何调用 java 函数(例如 abc.printabc())
请指教
你可以看看DWR which gives you methods from java as javascript function. Directly taken from their Tutorial:
public class Remote {
public String getData(int index) { ... }
}
以及相应的 html 片段:
<head>
<!-- other stuff -->
<script type="text/javascript" src="[WEBAPP]/dwr/interface/Remote.js"> </script>
<script type="text/javascript" src="[WEBAPP]/dwr/engine.js"> </script>
<script>
function myRemoteMethod() {
Remote.getData(42, {
callback: function(str) {
alert(str);
}});
}
</script>
</head>
<body>
<!-- ... -->
<a href="javascript:void(0);" onclick="myRemoteMethod()">Add Acnt</a>
<!-- ... -->
</body>
确保将 DWR 集成到 struts,他们也有相关的教程:DWR Struts Tutorial。
另一个解决方案是通过 ajax 请求调用 java 方法:
function myRemoteMethod() {
$.get('/link/to/method/')
.done(function (data) {
alert('Return message of server was:' + data);
});
}
这是一个 jQuery 示例,因此请确保您的 html 代码的 <head>
中包含该库。在 html 代码中调用此方法,然后通过 ajax.
<a href="javascript:void(0);" onclick="myRemoteMethod()">Add Acnt</a>