【发布时间】:2019-09-14 20:31:14
【问题描述】:
我正在尝试从我的 .xhtml 页面向我的支持 ManagedBean 函数发起 Ajax 调用,如下所示:
.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;
}
上面的代码工作正常,因为每次我触发命令按钮时,都会将一个新的“Cell”对象添加到列表中并在我的表单中异步呈现。
我使用棘手的方式从javascript触发commandButton(在我看来这不是最好的选择)
document.getElementById('jsfform:button').onclick();
我现在想要实现的目标是做一些类似的事情,但 cellBean.children 函数有参数(fe 一个字符串列表),将它传递给支持 bean 函数并做一些事情,我应该怎么做做?显然我不能像以前那样触发 commandButton,因为我不能传递这样的参数
例子:
<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;
}
提前致谢。
=============== 更新 ==================
按照example 到目前为止我所尝试的是:
.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()}" />
<f:ajax render=":ajaxform"/>
</h:commandButton>
Javascript 代码
// json1 && json2 are two json strings
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();
托管 Bean
private String childCellsStr;
private String parentCellsStr;
//getters && setters
不幸的是,即使设置了 inputHidden 字段的值,支持 bean 设置器也没有被触发并且值为 null
【问题讨论】:
-
你尝试了吗?参数内容应该从哪里来?这些 QA 可能会有所帮助:Passing parameter to JSF action 或 How to pass JavaScript variables as parameters to JSF action method?
-
参数内容已经在一个javascript变量中,资源看起来很有帮助,会去看看谢谢
-
嗨@Selaron,我已按照您在 cmets 部分提供的示例进行操作,但没有按预期工作,您能否查看我更新的问题?
-
并且调用了action方法? ypu 是否调试了 http 请求?价值观在吗?如果您(用于测试)使然后不隐藏怎么办?
标签: javascript java ajax jsf primefaces