问题
How do I create ofer_has_location objects (join object from location and ofer) using the current ofer and the selected items from the h:selectManyCheckBox
<h:selectOneMenu id="companyidCompany"
value="#{oferController.selected.companyidCompany}"
title="#{bundle.CreateOferTitle_companyidCompany}"
required="true"
requiredMessage="#{bundle.CreateOferRequiredMessage_companyidCompany}">
<f:ajax event="valueChange" execute="companyidCompany"
render="locationCollection" />
<f:selectItems value="#{companyController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="#{bundle.CreateOferLabel_locationCollection}"
for="locationCollection" />
<h:selectManyListbox id="locationCollection" value="locations"
title="#{bundle.CreateOferTitle_locationCollection}">
<c:forEach items="locations">
<f:selectItems var="locations"
value="#{oferController.selected.companyidCompany.locationCollection}" />
</c:forEach>
</h:selectManyListbox>
回答1:
What you need to do in order to achieve 'connected elements' functionality:
- Have two elements (
<h:selectOneMenu>and<h:selectManyLisBox>in your case), where the second one will be dependent on the selected option(s) of the first one. The second element must have an id in order to be rerendered afterwards. - Every HTML
selectelement (that's rendered by both JSF tags of your choice) will have a set ofoptions that are not supposed to be created via iterative element like<c:forEach>(though, it is in fact possible), but rather via the<f:selectItem>/<f:selectItems>tags (thus, remove your iterative tag in comment). - When values in the components are bound not as plain
Strings, or primitive wrappers (Integer, etc.), but rather as model objects (YourClassobjects, etc.), then you need to tell JSF two things: how can it printoption'svaluefrom your class and how can it reconstruct an object from request parameter that is a string. For this you need to implement Converter, that is, explain JSF how to do the abovementioned transformations. Use this answer and BalusC's blog as reference points. Note the appropriate syntax for<f:selectItems itemValue="...">here. - Model values bound by these two components also need to represent your classes, just in a same way as selected items' values. For
<h:selectOneMenu>it isvalue="#{}"; for<h:selectManyListbox>it isvalue="#{}"withYourClass selectOneMenuValueandList<YourClass> selectManyListboxValuesorYourClass[] selectManyListboxValuesbean properties respectively. - Population of second
selectwill be handled via<f:ajax>tag. As contents need to be calculated 'on the fly', the right spot to make it is within itslistenerattribute (i.e. to haveList<YourClass> contentsOfSecondListbox = createListboxValues(YourClass oneMenuSelectedOption);) . As you'd desire to rerender the second element, specify its client id inrenderattribute of<f:ajax>. Example here.
In case you are binding, for example, to String/String[] values, you won't need the converter parts.
Try to go through it step by step to find out your errors and correct them.
来源:https://stackoverflow.com/questions/16289213/how-to-implement-foreach-in-jsf