如何在jsf中实现foreach? [英] How to implement foreach in jsf?

查看:82
本文介绍了如何在jsf中实现foreach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用当前要约和从h:selectManyCheckBox中选择的项目创建要约对象(从位置和要约连接对象)

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>

推荐答案

要实现连接的元素"功能需要做的事情:

What you need to do in order to achieve 'connected elements' functionality:

  1. 具有两个元素(在您的情况下为<h:selectOneMenu><h:selectManyLisBox>),其中第二个元素将取决于第一个元素的所选选项.第二个元素必须具有一个ID,以便以后重新呈现.
  2. 每个HTML select元素(由您选择的两个JSF标记呈现)都会有一组option,但不应通过像<c:forEach>这样的迭代元素来创建(尽管实际上可能),而是通过<f:selectItem>/<f:selectItems>标签(因此,请删除注释中的迭代标签).
  3. 当组件中的值不是以普通的String或原始包装(Integer等)绑定,而是以模型对象(YourClass对象等)绑定时,则需要告知JSF有两件事:如何从您的类中打印optionvalue,以及如何从请求参数(字符串)中重建对象.为此,您需要实现 Converter ,也就是说,解释JSF如何进行上述转换.使用此答案 此处的适当语法.
  4. 由这两个组件绑定的模型值也需要代表您的类,就像所选项目的值一样.对于<h:selectOneMenu>,它是value="#{}";对于<h:selectManyListbox>,它是分别具有YourClass selectOneMenuValueList<YourClass> selectManyListboxValuesYourClass[] selectManyListboxValues bean属性的value="#{}".
  5. 第二个select的填充将通过<f:ajax>标签处理.由于需要``即时''计算内容,因此使其正确的位置位于其listener属性内(即具有List<YourClass> contentsOfSecondListbox = createListboxValues(YourClass oneMenuSelectedOption);).如要重新渲染第二个元素,请在<f:ajax>render属性中指定其客户端ID.示例此处.
  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.

例如,如果要绑定到String/String[]值,则不需要转换器部件.

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.

这篇关于如何在jsf中实现foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆