如何使用 a4j:ajax 或 a4j:actionListener 从 javascript 函数调用 bean 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6761263/
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
How to call bean method from javascript function using a4j:ajax or a4j:actionListener?
提问by Lan
I am using JSF, richfaces 4 stuff and i want to call a bean method when javascript function trigger.
我正在使用 JSF、richfaces 4 的东西,我想在 javascript 函数触发时调用 bean 方法。
回答by Ken Chan
You can use <a4j:jsFunction>
, which allows you to perform Ajax requests to invoke the bean method directly from JavaScript code , and the response can be returned in a JSON format to use in a client JavaScript calls.
您可以使用 <a4j:jsFunction>
,它允许您执行 Ajax 请求以直接从 JavaScript 代码调用 bean 方法,并且可以以 JSON 格式返回响应以在客户端 JavaScript 调用中使用。
Please note that <a4j:jsFunction>
is required to be inside a <h:form>
.
For example, you define the a4j:jsFunction likes this :
请注意,<a4j:jsFunction>
必须在<h:form>
. 例如,您像这样定义 a4j:jsFunction:
<h:form>
<a4j:jsFunction name="myJsFunction" action="#{bean.someAction}" reRender="xxxxx"/>
</h:form>
A javascript function which name called myJsFunction()
is created and it will invoke someAction() on the bean when being called.
创建一个名为调用的 javascript 函数,myJsFunction()
它会在被调用时调用 bean 上的 someAction()。
For example , in your javascript function :
例如,在您的 javascript 函数中:
<script type="text/javascript">
function yourJavaScriptFuntion() {
..............;
myJsFunction(); //bean.someAction() will invoke here
..............;
}