从JavaScript调用托管bean方法,最后填充Primefaces对话框组件

时间:2013-07-02 10:03:29

标签: java javascript jsf primefaces managed-bean

所以我遇到了集成纯JavaScript和primefaces对话框组件的问题。 用户应该获得一个叠加对话框(单个)作为对点击许多动态JavaScript生成的Div元素(多个)的响应。对话框的内容应取决于用户点击的div元素。

模仿问题的简化结构如下:

javascript(谷歌关闭):

// this code is in a loop that creates multiple div elements
// in the page dom structure.

var iconDiv = goog.dom.createDom('div', {
        'id': nodeId, // **
        'class': 'icondiv',
        'style': iconDivStyle   // **
    }, goog.dom.createDom('img', {
        'src': 'layer_icons/unused.png',
        'class': 'iconDivImg',
        'style': 'width:100% ;position:absolute; height: auto; '
    }));

       goog.events.listen(iconDiv,goog.events.EventType.CLICK,function (e){
    // in an ideal situation, the stringField is set to 'this.id' here ...
            dlg1.show();
        });

//** these are the code lines that mark the difference between the
//   divs created in the loop.

primefaces对话框:

这是用户点击任何图标时显示的对话框组件 在上面的javascript中创建。

<p:dialog id="DialogId" dynamic="true" widgetVar="dlg1"> 
    <h:form id="formId">
    <p:outputLabel id="clickedOnDiv_Id" value=" #{managedBean.stringField}"/>
<p:outputLabel id="somePropertyOfThisDiv_Id" value="#{managedBean.stringFieldSetByMethod}"/>
    </h:form>
</p:dialog>

Managed Bean:

class managedBean {

private String stringField ;  // getter and setter

private String stringFieldSetByMethod ;  // getter and setter

public void method(){

// uses the stringField in some way to set 'stringFieldSetByMethod' ...

}

}

以上是我想要实现的目标,如果你已经想出实现它的方法,请指教。

接下来是我到目前为止所尝试的内容:

我添加了以下javascript函数:

populateDialog = function(divId){
document.getElementById('formId:hiddenInput').value = divId;
document.getElementById('formId:hdnBtn').click();
}

所以onClick事件处理程序更改为以下内容:

goog.events.listen(iconDiv,goog.events.EventType.CLICK,function (e){
divId = this.id;   // where divId is a variable accessible to populateDialog()
dlg1.show();
        });  // this is inside a loop, so it is set for each created div

然后我将populateDialog()添加为对话框的OnShow回调并将其更改为:

<p:dialog id="DialogId" dynamic="true" widgetVar="dlg1" onShow="populateDialog();">
       <h:form id="formId">
             <h:inputHidden id="hiddenInput" value="#{nodesBean.stringField}" />
             <p:commandButton id="hdnBtn" ajax="true" actionListener="#{managedBean.method()}" style="display: none;" update=":formId"/>

        <p:outputLabel id="clickedOnDiv_Id" value="#{managedBean.stringField}"/>
        <p:outputLabel id="somePropertyOfThisDiv_Id" value="#{managedBean.stringFieldSetByMethod}"/>
       </h:form>
</p:dialog>

结果是从不调用managedBean.method,加载对话框时所有字段都为空,我可以按照进度     在调用populateDialog()之前的JavaScript,divId是正确的,我没有得到JavaScript错误。然而,服务器是完全无能为力的     关于我正在做的所有客户端的驼峰和跳跃,对真正发生的事情的解释将非常感激。

提前致谢!

1 个答案:

答案 0 :(得分:2)

所以解决方案正在使用:

对话框中的

`<h:inputHidden id="hiddenInput" value="#{nodesBean.stringField}" />
<p:remoteCommand name="remoteCommandFunction" process="hiddenInput" update=":formId"/>`

在onclick处理程序中(每个动态创建的div)

goog.events.listen(iconDiv,goog.events.EventType.CLICK,function (e){
divId = this.id;   // where divId is a variable accessible to populateDialog()
dlg1.show();
    });  // this is inside a loop, so it is set for each created div

javascript函数称为对话框onshow回调:

populateDialog = function(divId){
document.getElementById('formId:hiddenInput').value = divId;
remoteCommandFunction(); // this is cool cause it solves my problem, not cool cause you really have to know what you're looking for in the documentation to find it >:-/
}

最后,在托管bean中:

class managedBean {

private String stringField ;  // getter and setter

private String stringFieldSetByMethod ;  // getter and setter

 public void setStringField(String sf){
         this.stringField = sf;

         method();
      }

public void method(){

// uses the stringField in some way to set 'stringFieldSetByMethod' ...

                   }

}