Primefaces在Javascript中获取更新的bean属性[重复]

问题描述 投票:1回答:2

这个问题在这里已有答案:

在我看来,我有一个ajax行为,一个监听器更新一个bean属性,然后一个“oncomplete”动作执行一个javascript方法

这是ajax事件:

<p:ajax event="rowDblselect" listener="#{backController.onRowDoubleClick}"
                                    oncomplete="openNewTab()"  />
<h:inputHidden id="hutchy" value="#{backController.productViewerUrl}" />

这是应该更新属性的bean方法:

public void onRowDoubleClick(final SelectEvent event) {

    RecordDTO currentRecordDTO  = (RecordDTO) event.getObject();
    setProductViewerUrl("https://www.google.com/search?q=" + currentRecordDTO.getName());
}

public String getProductViewerUrl() {
    return productViewerUrl;
}

public void setProductViewerUrl(String productViewerUrl) {
    this.productViewerUrl = productViewerUrl;
}

此后,使用更新属性的javascript方法:

function openNewTab(){
	var url = $('#pbm\\:hutchy').val();
	var hiddenCode = "#{backController.productViewerUrl}";
	alert(url + hiddenCode);
	window.open(url, '_newtab');
}

问题是Javascript代码没有得到属性的更新值(即使是隐藏字段),我在DoubleClick事件后做了一些调试,我发现执行没有通过getter方法传递执行JS时的属性(警报前)

有没有人有想法?提前致谢!

javascript jsf primefaces
2个回答
0
投票

尝试使用<f:param name="productViewerUrl" value="#{backController.productViewerUrl}" />然后获取Bean中的值

FacesContext fc = FacesContext.getCurrentInstance();
    Map<String, String> params = fc.getExternalContext().getRequestParameterMap();

    this.productViewerUrl= params.get("productViewerUrl");

或绑定数据表中的值


0
投票

我找到了一个非常好的解决方案,只是在ajax事件之后对隐藏字段执行更新,以便视图考虑bean属性的新值,如:

<p:ajax event="rowDblselect" listener="#{backController.onRowDoubleClick}"
                               update="form:hutchy" oncomplete="openNewTab()"  />
© www.soinside.com 2019 - 2024. All rights reserved.