简体   繁体   English

使用javascript参数对ajax回调进行Primeface

[英]Primefaces ajax callback with parameter to javascript

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. 在handleComplete中,javascript函数args未定义,但不是xhr和status。 javascript function definition: 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 首先警告它给我页面,第二个解析错误,错误是:Uncaught TypeError:无法读取未定义的属性'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 . 并使用args.firstParam在javascript中访问它。

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. 然后得到这个元素的innerHTML,因为我将使用innerHTML获得的内容与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. 我想知道为什么args对象是未定义的,或者我怎样才能从javascript中管理bean属性。 Thank you. 谢谢。

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. 如果不是,您可能希望将process属性添加到按钮中,并将进程设置为@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. 我会在actionListener中调用我的方法而不是在行动中,并将()放在最后。 Differences between action and actionListener 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值不为null!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM