为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

缥缈止盈 2022-10-20 16:59:32
我正在尝试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;}提前致谢。
查看完整描述

1 回答

?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

我所要做的就是execute在标签中添加参数(按照本教程)。现在调用 setter 并且支持 bean 采用我想要的值


.xhtml 代码


<h:form id = "jsfform">

                <h:inputHidden id="childCells" value="#{cellBean.childCellsStr}" />

                <h:inputHidden id="parentCells" value="#{cellBean.parentCellsStr}" />

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

                    <f:actionListener binding="#{cellBean.checkBtnDisable()}" /> <!--first call the children function, then call the checkBtnDisable -->

                    <f:ajax execute="childCells parentCells" render=":ajaxform"/>

                </h:commandButton>

            </h:form>

Javascript代码


    document.getElementById('jsfform:childCells').value = json1;

document.getElementById('jsfform:parentCells').value = json2;

//trigger the button of the form with javascript

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

爪哇豆


private String childCellsStr;

private String parentCellsStr;

//getters && setters


查看完整回答
反对 回复 2022-10-20
  • 1 回答
  • 0 关注
  • 61 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号