How to implement foreach in jsf?

旧巷老猫 提交于 2019-12-24 23:25:12

问题


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:

  1. 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.
  2. Every HTML select element (that's rendered by both JSF tags of your choice) will have a set of options 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).
  3. When values in the components are bound not as plain Strings, or primitive wrappers (Integer, etc.), but rather as model objects (YourClass objects, etc.), then you need to tell JSF two things: how can it print option's value from 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.
  4. 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 is value="#{}"; for <h:selectManyListbox> it is value="#{}" with YourClass selectOneMenuValue and List<YourClass> selectManyListboxValues or YourClass[] selectManyListboxValues bean properties respectively.
  5. Population of second select will be handled via <f:ajax> tag. As contents need to be calculated 'on the fly', the right spot to make it is within its listener attribute (i.e. to have List<YourClass> contentsOfSecondListbox = createListboxValues(YourClass oneMenuSelectedOption);) . As you'd desire to rerender the second element, specify its client id in render attribute 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!