javascript Primefaces ajax回调,参数为javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20546264/
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
Primefaces ajax callback with parameter to javascript
提问by Mihai Serban
I am struggling to send a Primefaces bean property to javascript directly in order to open a new page and write the content of the bean property into the new page.
我正在努力将 Primefaces bean 属性直接发送到 javascript 以打开一个新页面并将 bean 属性的内容写入新页面。
I am using Primefaces 4.0.
我正在使用 Primefaces 4.0。
I have a command button like this:
我有一个这样的命令按钮:
<p:commandButton id="buttonId" update="buttonId, otherComponentId"
value="press me" process="@this" actionListener="#{homeController.myMethod}"
oncomplete="handleComplete(xhr, status, args)">
</p:commandButton>
In handleComplete javascript function args is undefined, but not xhr and status. javascript function definition:
在 handleComplete javascript 函数 args 是未定义的,但不是 xhr 和状态。javascript函数定义:
function handleComplete(xhr, status, args){
var w = window.open();
w.document.open();
alert(xhr.responseText.substring(100));
alert(status);
alert(args);
var d = '<head></head><body>'+ args.firstParam +'<body>';
w.document.write(d);
w.document.close();
}
first alert it's giving me the page, the second one parse error, and the error is: Uncaught TypeError: Cannot read property 'firstParam' of undefined
第一个警报它给了我页面,第二个解析错误,错误是:未捕获的类型错误:无法读取未定义的属性“firstParam”
I want to pass in the args a string like this:
我想在 args 中传递一个这样的字符串:
public String MyMethod() {
RequestContext context = RequestContext.getCurrentInstance();
context.addCallbackParam("firstParam", "my string");
return "";
}
and access it in javascript with args.firstParam
.
并在 javascript 中访问它args.firstParam
。
the method is called, (I have some printscreens that work.)
该方法被调用,(我有一些可以工作的打印屏幕。)
I have to try this way and not to set the text into a
我必须尝试这种方式,而不是将文本设置为
<h:outputText id="myText" escape="false" rendered="true"
value="#{homeController.property}" />
and then get innerHTML of this element because what I will get with innerHTML will not be the same as the string variable in the bean. This method works but not as I would like. I am wondering why the args object is undefined or how else could I get the bean property manageable from javascript. Thank you.
然后得到这个元素的innerHTML,因为我用innerHTML得到的将与bean中的字符串变量不同。这种方法有效,但不是我想要的。我想知道为什么 args 对象未定义,或者我如何才能从 javascript 获得可管理的 bean 属性。谢谢你。
回答by Hatem Alimam
First thing you should make sure that your method is called. Put some logging or debug your method if it's called correctly.
首先你应该确保你的方法被调用。如果调用正确,则记录或调试您的方法。
If not you might want to add the process attribute into the button, and set the process into @this.
如果不是,您可能希望将流程属性添加到按钮中,并将流程设置为@this。
If in this stage your method is called, then you have a validation error in your page.
如果在此阶段调用了您的方法,则您的页面中存在验证错误。
And I would call my methods in actionListener not in action, and put the () in the end. Differences between action and actionListener
我会在 actionListener 中调用我的方法而不是在 action 中,并将 () 放在最后。action 和 actionListener 的区别
Try the following
尝试以下
<p:commandButton id="buttonId"
value="press me" actionListener="#{homeController.myMethod()}"
process="@this"
oncomplete="handleComplete(xhr, status, args)">
</p:commandButton>
Since your error is "firstParam" is undefined and not the "args" is undefined, you might want to make sure that your firstParam value is not null !.
由于您的错误是“firstParam”未定义而不是“args”未定义,您可能需要确保您的 firstParam 值不为空!