本文介绍了如何在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:
- 具有两个元素(在您的情况下为
<h:selectOneMenu>
和<h:selectManyLisBox>
),其中第二个元素将取决于第一个元素的所选选项.第二个元素必须具有一个ID,以便以后重新呈现. - 每个HTML
select
元素(由您选择的两个JSF标记呈现)都会有一组option
,但不应通过像<c:forEach>
这样的迭代元素来创建(尽管实际上可能),而是通过<f:selectItem>
/<f:selectItems>
标签(因此,请删除注释中的迭代标签). - 当组件中的值不是以普通的
String
或原始包装(Integer
等)绑定,而是以模型对象(YourClass
对象等)绑定时,则需要告知JSF有两件事:如何从您的类中打印option
的value
,以及如何从请求参数(字符串)中重建对象.为此,您需要实现 Converter ,也就是说,解释JSF如何进行上述转换.使用此答案和 BalusC的博客作为参考点.请注意<f:selectItems itemValue="...">
此处的适当语法. - 由这两个组件绑定的模型值也需要代表您的类,就像所选项目的值一样.对于
<h:selectOneMenu>
,它是value="#{}"
;对于<h:selectManyListbox>
,它是分别具有YourClass selectOneMenuValue
和List<YourClass> selectManyListboxValues
或YourClass[] selectManyListboxValues
bean属性的value="#{}"
. - 第二个
select
的填充将通过<f:ajax>
标签处理.由于需要``即时''计算内容,因此使其正确的位置位于其listener
属性内(即具有List<YourClass> contentsOfSecondListbox = createListboxValues(YourClass oneMenuSelectedOption);
).如要重新渲染第二个元素,请在<f:ajax>
的render
属性中指定其客户端ID.示例此处.
- 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
select
element (that's rendered by both JSF tags of your choice) will have a set ofoption
s 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
String
s, or primitive wrappers (Integer
, etc.), but rather as model objects (YourClass
objects, etc.), then you need to tell JSF two things: how can it printoption
'svalue
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. - 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 selectOneMenuValue
andList<YourClass> selectManyListboxValues
orYourClass[] selectManyListboxValues
bean properties respectively. - 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 itslistener
attribute (i.e. to haveList<YourClass> contentsOfSecondListbox = createListboxValues(YourClass oneMenuSelectedOption);
) . As you'd desire to rerender the second element, specify its client id inrender
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!