猿问

使用JSF中的javascript传递的变量触发commandButton动作

我正在尝试Ajax从我的 . xhtml页面到我的支持ManagedBean功能,如下所示:


.xhtml 表单代码


            <h:form id = "jsfform">

                <h:commandButton id="button" action="#{cellBean.children()}">

                    <f:ajax render=":results" />

                </h:commandButton> </h:form>

            <h:panelGroup id="results">

                <th>Child cells</th>

                <td>

                <ui:repeat value="#{cellBean.childCells}" var="cell">

                        <tr>#{cell.cellName}</tr>

                </ui:repeat>

                </td>

            </h:panelGroup>

托管bean的功能代码


 public List<Cell> children(){

    this.childCells.add(new Cell("cell1"));

    return this.childCells;

}

上面的代码工作正常,因为每次我触发 commandButton 时,都会将一个新的“ Cell”对象添加到列表中并在我的表单中异步呈现。


我使用棘手的方式触发commandButtonfrom javascript(在我看来这不是最好的选择)


document.getElementById('jsfform:button').onclick();

我现在想要实现的是做一些类似的事情,但cellBean.children函数有参数(fe 一个字符串列表),将它传递给支持 bean 函数并做一些事情,我该怎么办?显然我不能像以前那样触发命令按钮,因为我不能传递这样的参数


例子:


        <h:form id = "jsfform">

            <h:commandButton id="button" action="#{cellBean.children(a_list)}">

                <f:ajax render=":results" />

            </h:commandButton> </h:form>

        <h:panelGroup id="results">

            <th>Child cells</th>

            <td>

            <ui:repeat value="#{cellBean.childCells}" var="cell">

                    <tr>#{cell.cellName}</tr>

            </ui:repeat>

            </td>

        </h:panelGroup>



public List<Cell> children(List<String> alist){

    this.childCells.add(new Cell("cell1"));

    return this.childCells;

}

提前致谢。


缥缈止盈
浏览 62回答 1
1回答

蝴蝶不菲

我所要做的就是execute在标签中添加参数(按照本教程)。现在调用 setter 并且支持 bean 采用我想要的值.xhtml 代码<h:form id = "jsfform">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h:inputHidden id="childCells" value="#{cellBean.childCellsStr}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h:inputHidden id="parentCells" value="#{cellBean.parentCellsStr}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h:commandButton id="button"&nbsp; style="display: none" action="#{cellBean.children()}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <f:actionListener binding="#{cellBean.checkBtnDisable()}" /> <!--first call the children function, then call the checkBtnDisable -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <f:ajax execute="childCells parentCells" render=":ajaxform"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </h:commandButton>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </h:form>Javascript代码&nbsp; &nbsp; document.getElementById('jsfform:childCells').value = json1;document.getElementById('jsfform:parentCells').value = json2;//trigger the button of the form with javascriptdocument.getElementById('jsfform:button').onclick();爪哇豆private String childCellsStr;private String parentCellsStr;//getters && setters
随时随地看视频慕课网APP

相关分类

Java
我要回答