Seam and JSF: How to post multiple values instead of single
We have this ui:repeat:
<ui:repeat value="#{notAssigned}" var="pair">
#{pair.data.processName}<br/>
<s:button value="assign" action="#{mypage.assign}">
<f:param name="taskId" valu开发者_如何学运维e="#{pair.task.id}"/>
</s:button>
</ui:repeat>
Now I can assign one and one elements but pushing the assign button. But I want to refactor this and add a checkbox, so that you can choose all that you want to assign, and then push one button that will assign all.
How can I modify this code to support checkbox
that contains the taskId
(which is a Long) and one button to submit.
UPDATE
I tried changing the code to use <h:selectManyCheckbox>
<h:selectManyCheckbox value="#{mypage.notAssignedTaskIds}">
<f:selectItem itemValue="#{pair.task.id}" itemLabel="#{messages['choose']}" />
</h:selectManyCheckbox>
#{pair.data.processName}
<h:commandButton action="#{mypage.test}" value="Assign"/>
And in my bean:
@Setter @Getter
List<String> notAssignedTaskIds = new ArrayList<String>();
public void test() {
log.info("Inside test, size of list #0", notAssignedTaskIds.size());
for(String id : notAssignedTaskIds) {
log.info("TaskId's chosen #0", id);
}
}
But notAssignedTaskids is always empty
My suggestion is, create a boolean flag in pair object. Then
<a:region>
<ui:repeat value="#{notAssigned}" var="pair">
<h:selectBooleanCheckbox value="#{pair.booleanFlag}">
#{pair.data.processName}
</ui:repeat>
<a:commandLink action="#{yourBean.assignAll()}" value="assignAll">
</a:commandLink>
</a:region>
In your bean, iterate over notAssigned list.
public void assignAll() {
.
.
.
for (Pair pair : notAssigned) {
if (pair.booleanFlag) {
mypage.assign(pair);
}
}
}
And modify your assign method to get a pair object. By the way you can do it without ajax support. Just remove region and convert your a:commandLink to h:commandLink.
If you don't want to alter your model, then add a Map<Long, Boolean>
to your bean.
private Map<Long, Boolean> selectedIds = new HashMap<Long, Boolean>();
Bind it to <h:selectBooleanCheckbox>
value with task ID as key.
<ui:repeat value="#{bean.notAssigned}" var="pair">
<h:selectBooleanCheckbok value="#{bean.selectedIds[pair.task.id]}" />
#{pair.data.processName}
</ui:repeat>
<s:button value="assign" action="#{bean.assign}" />
Collect the selected values in assign()
method.
List<Pair> selectedPairs = new ArrayList<Pair>();
for (Pair pair : notAssigned) {
if (selectedIds.get(pair.getTask().getId())) {
selectedPairs.add(pair);
}
}
I reckon you want a h:selectManyCheckbox element. Check out the docs here.
精彩评论