【问题标题】:How to get input component's submitted value in backing component如何在支持组件中获取输入组件的提交值
【发布时间】:2013-04-17 09:53:59
【问题描述】:

我制作了一个带有支持UIInput 的复合组件。它包含一个数字微调器。更改微调器时,新值不会发送到支持组件。

我已经简化了情况(似乎不需要支持,但是问题仍然存在)。

Sytem.out.println 突出了问题。

复合组件:

<cc:interface componentType="periodInput" >
    <cc:attribute name="value" type="org.joda.time.Period" />
</cc:interface>

<cc:implementation>
    <p:spinner id="count" min="0" binding="#{cc.countComponent}" converter="javax.faces.Integer" label="Every "/>
</cc:implementation>


支持组件:

@FacesComponent("periodInput")
public class PeriodBacking extends UIInput implements NamingContainer {

    private UIInput countComponent;
    // And getter & setter.

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        Period period = (Period) getValue();
        if(period == null) {
            period = Period.weeks(1).withPeriodType(PeriodType.weeks());
        }
        int count;
        count = period.get(period.getFieldTypes()[0]);
        countComponent.setValue(count);
        super.encodeBegin(context);
    }

    @Override
    public Object getSubmittedValue() {
        return this;
    }

    @Override
    protected Object getConvertedValue(FacesContext context, Object newSubmittedValue) {
        // PROBLEM: Always prints out '1':
        System.out.println("Count: " + count); 
        int count = (Integer) countComponent.getValue();
        Period totalPeriod = new Period(0).withDays(count);
        return totalPeriod;
    }

    @Override
    public String getFamily() {
        return UINamingContainer.COMPONENT_FAMILY;
    }
}

复合组件的使用方式如下:

<custom:Period value="#{cc.attrs.trackedproduct.samplePeriod}" />

trackedproduct 出现在 @ViewScoped bean 中。

【问题讨论】:

    标签: jsf jsf-2 composite-component


    【解决方案1】:
    int count = (Integer) countComponent.getValue();
    

    您应该获得提交的值,而不是模型值。模型值此时(在转换/验证阶段)尚未被提交/转换/验证的值更新。

    int count = Integer.valueOf((String) countComponent.getSubmittedValue());
    

    与具体问题无关,您的getSubmittedValue()getConvertedValue() 未正确实施。应该这样做:

    @Override
    public Object getSubmittedValue() {
        return countComponent.getSubmittedValue();
    }
    
    @Override
    protected Object getConvertedValue(FacesContext context, Object newSubmittedValue) {
        int count = Integer.valueOf((String) newSubmittedValue);
        Period totalPeriod = new Period(0).withDays(count);
        return totalPeriod;
    }
    

    另见:

    【讨论】:

      猜你喜欢
      • 2016-05-17
      • 2022-01-05
      • 2023-03-27
      • 2017-11-01
      • 1970-01-01
      • 2019-10-27
      • 2022-01-12
      • 2013-03-13
      相关资源
      最近更新 更多