I am trying to make an Ajax
call from my .xhtml
page to my backing ManagedBean
function as shown below:
.xhtml form code
<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>
managed bean's function code
public List<Cell> children(){
this.childCells.add(new Cell("cell1"));
return this.childCells;
}
The above code works fine since each and every time i trigger the commandButton, a new "Cell
" object is added into the list and rendered asynchronously in my form.
I trigger the commandButton
from javascript using the tricky way (which is does not seem to me the best option)
document.getElementById('jsfform:button').onclick();
What I want to achieve now is do something similar but with cellBean.children
function having parameters (f.e a list of Strings), pass it to the backing bean function and do stuff, what should I do? Obviously I cannot trigger the commandButton as I did previously because I cannot pass parameters like that
Example:
<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;
}
Thanks In advance.
=============== UPDATE ==================
Following this example what I have tried so far is:
.xhtml form
<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 code
// 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();
Managed Bean
private String childCellsStr;
private String parentCellsStr;
//getters && setters
Unfortunately, even the values of the inputHidden
fields are set, the backing bean setters are not triggered and the values are null