JSF master / detail question
[nobr]In my app I have a:
- a selectone listbox
- a details select many listbox, the contents of which are dependent upon the selected item in the select one listbox
- a command button which acts process the items from the selectmany listbox
the backing bean is in request scope. The actual JSP and backing bean are attached below:
my problem:
- when I click the command button I get validation errors caused by the selectmany listbox during the validation phase. This is because:
-- the backing bean is in request scope and a new instance is created, but the appropriate items have not been filled yet, because the update model values phase has not been reached yet.
I feel like I'm on the wrong track altogether. Can someone point me in the right direction about how I should approach this problem? Thanks.
-- JSP
<f:view>
<f:loadBundle basename="messages" var="msgs"/>
<html>
<head>
</head>
<body>
<h:form>
<h:outputText value="#{msgs.master}"/>
<br>
<h:message for="masterListbox"/>
<h:selectOneListbox onchange="submit()" immediate="true" value="#{bb.selectedMasterId}">
<f:selectItems value="#{bb.masterRecords}"/>
</h:selectOneListbox>
<br>
<h:outputText value="#{msgs.details}"/>
<h:message for="detailsListbox"/>
<h:selectManyListbox value="#{bb.selectedDetailIds}">
<f:selectItems value="#{bb.detailRecords}"/>
</h:selectManyListbox>
<br>
<h:commandButton value="#{msgs.processDetails}" actionListener="#{bb.processDetails}"/>
</h:form>
</body>
</html>
</f:view>
-- Backing Bean
publicclass BackingBean{
private IDao dao;
private List<SelectItem> masterRecords;
private List<SelectItem> detailRecords;
private Integer selectedMasterRecordId;
private Integer[] selectedDetailRecordIds =new Integer[0];
public List<SelectItem> getMasterRecords(){
return this.masterRecords;
}
public List<SelectItem> getDetailRecords(){
return this.detailRecords;
}
// the dao is injected through the managed bean facilities
publicvoid setDao(IDao dao){
this.dao = dao;
this.masterRecords = dao.getMasterRecords();
// by default the first record will be preselected
this.selectedMasterRecordId = (Integer)this.masterRecords.get(0).getValue();
this.detailRecords = dao.getDetailRecords(this.selectedMasterRecordId);
}
public Integer[] getSelectedDetailIds(){
return this.selectedDetailRecordIds;
}
publicvoid setSelectedDetailIds(Integer[] selectedDetailRecordIds){
this.selectedDetailRecordIds = selectedDetailRecordIds;
}
public Integer getSelectedMasterId(){
return this.selectedMasterRecordId;
}
publicvoid setSelectedMasterId(Integer newMasterId){
this.selectedMasterRecordId = newMasterId;
// this is a good time to set the details.
this.detailRecords = dao.getDetailRecords(this.selectedMasterRecordId);
}
publicvoid processDetails(ActionEvent actionEvent){
// do some processing. But this is never reached due to validation errors on the details
// selectManyListbox
}
}
[/nobr]

