我制作了一个带有支持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
bean中存在@ViewScoped
的位置。
答案 0 :(得分:3)
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;
}