Question

I have a jsf page with multiple radiobutton groups (dynamically generated) on it. I need to retrieve the values from it in a backing bean, but fails to do so.

The business: a user wants to subscribe to a course that consists of multiple groups of coursedays. The user can choose the coursedays. So if a course consists of for example 4 coursedays, organised in 3 different groups, the user can choose from 12 coursedays, in blocks of 3.

The relevant part of the xhtml-page:

<c:forEach var="cd1" items="#{coursedayBean.getCoursedays(groupBean.getFirstGroup}">
  <h:selectOneRadio value="#{subscriptionBean.selectedCoursedays[cd1.sequenceNr]}" >
    <f:selectItems value="#{coursedayBean.getCoursedaysSelectItems}"/>
  </h:selectOneRadio>
</c:forEach>

This results in a n*m matrix of radiobuttons where I want to retrieve n values. The selectItems are of type <Long, String>.

In my backing bean, I declared the following:

public List<String> getSelectedCoursedays() {
  return selectedCoursedays;
}

public void setSelectedCoursedays(List<String> selectedCoursedays) {
  this.selectedCoursedays = selectedCoursedays;
}

I tried with a Map, List, but none of them worked. The setSelectedCoursedays is never called. How do I declare the array/list/map to get the values in my backing bean?

#{subscriptionBean.selectedCoursedays[cd1.sequenceNr]}

doesn't do the trick.

Was it helpful?

Solution 2

It is miss understanding between c:forEach and ui:repeat. c:forEach will not build UI component tree nodes. Firstly, you have to reference difference between them here.

OTHER TIPS

This construct should work just fine. The setter will indeed never be called. JSF/EL just calls the setter on ArrayList itself by the add(index, object) method. I.e. it does basically:

subscriptionBean.getSelectedCoursedays().add(cd1.sequenceNr, selectedItem);

I'm not sure how you observed the concrete problem of "it doesn't work". Perhaps you were firing an ajax request and putting a breakpoint on the setter method and didn't read the server logs. There are two possible cases where this construct will fail.

  1. If you don't prepare the selectedCoursedays with new ArrayList(), then you will get PropertyNotFoundException: Target Unreachable, 'selectedCoursedays' returned null.

  2. If you don't fill the selectedCoursedays with the same amount of null items as the course days, then you will get an ArrayIndexOutOfBoundsException

So, you should prepare the selectedCoursedays as follows:

@PostConstruct
public void init() {
    selectedCoursedays = new ArrayList<String>();
    for (int i = 0; i < coursedays.size(); i++) {
        selectedCoursedays.add(null);
    }
}

Easier alternative is to make it a String[].

private String[] selectedCoursedays;

@PostConstruct
public void init() {
    selectedCoursedays = new String[coursedays.size()];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top