Primefaces AJAX回调参数的JavaScript [英] Primefaces ajax callback with parameter to javascript

查看:261
本文介绍了Primefaces AJAX回调参数的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力直接以打开新的一页,写的bean属性的内容到新的页面发送Primefaces bean属性的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 4.0。

I am using 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>

在handleComplete的javascript函数参数是不​​确定的,但不是XHR和地位。 javascript函数的定义:

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();
}

第一个警报它给我的网页,第二个语法错误, 而错误是:未捕获的类型错误:无法读取属性firstParam未定义

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

我要在args设置为传递一个字符串是这样的:

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

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

和访问它的JavaScript与 args.firstParam

and access it in javascript with args.firstParam .

该方法被调用,(我有一些printscreens这项工作。)

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}" />

,然后得到,因为我将与innerHTML的得到的将不会是相同的bean中的字符串变量这个元素的innerHTML。此方法有效,但并不如我所愿。我很奇怪,为什么ARGS对象是不确定的或者我怎么回事,能得到bean的属性从JavaScript管理。谢谢你。

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.

推荐答案

首先你要确保你的方法被调用。 把一些日志记录或调试方法,如果它被称为正确。

First thing you should make sure that your method is called. Put some logging or debug your method if it's called correctly.

如果不是你可能要添加的过程属性进按钮, 并设置过程分为@this

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.

和我所说的不是行动我的ActionListener的方法,并把()到底。 <一href="http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener">Differences行动之间的ActionListener

And I would call my methods in actionListener not in action, and put the () in the end. Differences between action and actionListener

请尝试以下

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

由于您的错误是firstParam是不确定的,而不是ARGS是不确定的,你可能想确保你的firstParam值不为null!

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

这篇关于Primefaces AJAX回调参数的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆