最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Primefaces ajax callback with parameter to javascript - Stack Overflow

programmeradmin3浏览0评论

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.

I am using Primefaces 4.0.

I have a mand button like this:

 <p:mandButton id="buttonId" update="buttonId, otherComponentId"
    value="press me" process="@this" actionListener="#{homeController.myMethod}"
    onplete="handleComplete(xhr, status, args)">
 </p:mandButton>

In handleComplete javascript function args is undefined, but not xhr and status. javascript function definition:

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

I want to pass in the args a string like this:

public String MyMethod() {
    RequestContext context = RequestContext.getCurrentInstance();  
    context.addCallbackParam("firstParam", "my string");
      return "";
}

and access it in javascript with 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.

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.

I am using Primefaces 4.0.

I have a mand button like this:

 <p:mandButton id="buttonId" update="buttonId, otherComponentId"
    value="press me" process="@this" actionListener="#{homeController.myMethod}"
    onplete="handleComplete(xhr, status, args)">
 </p:mandButton>

In handleComplete javascript function args is undefined, but not xhr and status. javascript function definition:

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

I want to pass in the args a string like this:

public String MyMethod() {
    RequestContext context = RequestContext.getCurrentInstance();  
    context.addCallbackParam("firstParam", "my string");
      return "";
}

and access it in javascript with 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.

Share Improve this question edited Dec 12, 2013 at 14:58 Mihai Serban asked Dec 12, 2013 at 14:32 Mihai SerbanMihai Serban 4052 gold badges7 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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.

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

Try the following

<p:mandButton id="buttonId"
 value="press me"  actionListener="#{homeController.myMethod()}"
 process="@this"
 onplete="handleComplete(xhr, status, args)">
</p:mandButton>

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 !.

发布评论

评论列表(0)

  1. 暂无评论