c:forEach中的绑定 [英] Bindings in c:forEach

查看:74
本文介绍了c:forEach中的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组项目,每个项目都有一个最小值,可能值和最大值.有一些验证器可以确保最大值大于最小值等.验证器使用绑定,以便他们可以比较用户输入的所有三个值.最初它们位于ui:repeat中,但是这会导致下拉菜单出现问题,我发现改用c:forEach可以解决此问题.不幸的是,它也弄乱了绑定.我希望有一种方法可以解决这些问题,而无需回到ui:repeat.

I have a collection of items which each have a min, likely, and max value. There are validators to ensure the maximum is greater than the minimum etc. The validators use bindings so they can compare all three values the user has entered. Originally they were in a ui:repeat, but that was causing trouble with a dropdown and I found switching to a c:forEach fixed it. Unfortunately, it's also messed up the bindings. I'm hoping there's a way to fix them without going back to the ui:repeat.

是否可以在c:forEach中具有绑定?有什么方法可以向绑定中添加id或varStatus索引,以便列表中的每个项目都不相同吗?例如在下面的示例中,我尝试过类似的操作

Is it possible to have a binding in a c:forEach? Is there some way to add an id or a varStatus index to the binding so it isn't the same for every item in the list? e.g. in the example below I've tried things like

<h:inputText id="impactMin_#{impact.id}" binding="#{impactMin_#{impact.id}}">
<h:inputText id="impactMin_#{impact.id}" binding="#{impactMin}_#{impact.id}">

但毫不奇怪,这些都不起作用.如果有人知道这样做的明智方法,我将不胜感激.

but unsurprisingly, those don't work. If anyone knows a sensible way of doing this, I'd be very grateful to hear it.

这是代码的简化版本:

<c:forEach id="impacts" var="impact" items="#{mybean.impacts}" varStatus="i">
    <h:panelGroup layout="block" id="impactContainer_#{impact.id}">
        <h:outputText value="#{impact.score_impact_type_idInterface.score_impact_type_name}"/>
        <h:panelGroup layout="block" class="row form-group" id="impactAssessment_#{impact.id}">
            <h:panelGroup layout="block">
                <h:inputText id="impactMin" value="#{impact.current_impact_min}" binding="#{impactMin}" label="Minimum">
                    <f:attribute name="impactMl" value="#{impactMl}"/>
                    <f:attribute name="impactMax" value="#{impactMax}"/>
                    <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" />
                    <f:validator binding="#{impactMinValidator}"/>
                </h:inputText>
            </h:panelGroup>
            <h:panelGroup layout="block">
                <h:inputText id="impactMl" value="#{impact.current_impact_ml}" binding="#{impactMl}" label="Most likely">
                    <f:attribute name="impactMin" value="#{impactMin}"/>
                    <f:attribute name="impactMax" value="#{impactMax}"/>
                    <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" />
                    <f:validator binding="#{impactLikelyValidator}"/>
                </h:inputText>
            </h:panelGroup>
            <h:panelGroup layout="block">
                <h:inputText id="impactMax" value="#{impact.current_impact_max}" binding="#{impactMax}" label="Maximum">
                    <f:attribute name="impactMin" value="#{impactMin}"/>
                    <f:attribute name="impactMl" value="#{impactMl}"/>
                    <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" />
                    <f:validator binding="#{impactMaxValidator}"/>
                </h:inputText>
            </h:panelGroup>
            <div class="col-sm-12">
                <h:message for="impactMin"/> 
                <h:message for="impactMl"/>
                <h:message for="impactMax"/>
            </div>
        </h:panelGroup>
    </h:panelGroup>
</c:forEach>

推荐答案

在尝试更好地理解绑定的同时,我找到了这个答案

While trying to get a better understanding of bindings in general, I found this answer JSF 2 composites and binding for validation . While not quite the same thing, I found it could be adapted to a c:forEach like this:

<c:forEach id="impacts" var="impact" items="#{mybean.impacts}" varStatus="i">
    <h:panelGroup layout="block" id="impactContainer_#{impact.id}">
        <h:outputText value="#{impact.score_impact_type_idInterface.score_impact_type_name}"/>
        <h:panelGroup layout="block" class="row form-group" id="impactAssessment_#{impact.id}">
            <ui:param name="impactMin" value="impactMin_#{impact.id}" />
            <ui:param name="impactMl" value="impactMl_#{impact.id}" />
            <ui:param name="impactMax" value="impactMax_#{impact.id}" />
            <h:panelGroup layout="block">
                <h:inputText id="impactMin_#{impact.id}" value="#{impact.current_impact_min}" binding="#{requestScope[impactMin]}" label="Minimum">
                    <f:attribute name="impactMl" value="#{requestScope[impactMl]}"/>
                    <f:attribute name="impactMax" value="#{requestScope[impactMax]}"/>
                    <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" />
                    <f:validator binding="#{impactMinValidator}"/>
                </h:inputText>
            </h:panelGroup>
            <h:panelGroup layout="block">
                <h:inputText id="impactMl_#{impact.id}" value="#{impact.current_impact_ml}" binding="#{requestScope[impactMl]}" label="Most likely">
                    <f:attribute name="impactMin" value="#{requestScope[impactMin]}"/>
                    <f:attribute name="impactMax" value="#{requestScope[impactMax]}"/>
                    <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" />
                    <f:validator binding="#{impactLikelyValidator}"/>
                </h:inputText>
            </h:panelGroup>
            <h:panelGroup layout="block">
                <h:inputText id="impactMax_#{impact.id}" value="#{impact.current_impact_max}" binding="#{requestScope[impactMax]}" label="Maximum">
                    <f:attribute name="impactMin" value="#{requestScope[impactMin]}"/>
                    <f:attribute name="impactMl" value="#{requestScope[impactMl]}"/>
                    <f:convertNumber locale="#{loginInfo.realLocale}" minIntegerDigits="1" maxFractionDigits="2" />
                    <f:validator binding="#{impactMaxValidator}"/>
                </h:inputText>
            </h:panelGroup>
            <div class="col-sm-12">
                <h:message for="impactMin_#{impact.id}"/> 
                <h:message for="impactMl_#{impact.id}"/>
                <h:message for="impactMax_#{impact.id}"/>
            </div>
        </h:panelGroup>
    </h:panelGroup>
</c:forEach>

请注意靠近顶部的ui:param位.这似乎可以解决问题.谢谢BalusC!

Note the ui:param bits near the top. That seems to do the trick. Thanks BalusC!

这篇关于c:forEach中的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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