我有一个复合组件:
<ui:component
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
<cc:interface componentType="compositeLocalDate">
<cc:attribute name="value" type="org.joda.time.LocalDate" shortDescription="The bean value" required="true" />
<cc:attribute name="days" type="java.lang.Boolean" shortDescription="Add the days input question" required="true" />
<cc:attribute name="disabledBy" type="java.lang.Boolean" shortDescription="Disable the component" required="false" />
<cc:attribute name="limitFuture" type="java.lang.Integer" shortDescription="How many future months to allow based on this number of days in the future. default = all" required="false" />
</cc:interface>
<cc:implementation><!--
--><div class="dateInput">
<h:inputText autocomplete="off" binding="#{cc.day}" id="day" maxlength="2" styleClass="compositeDay" disabled="#{cc.attrs.disabledBy}" a:placeholder="dd" a:pattern="[0-9]{1,2}" rendered="#{cc.attrs.days}" />
<h:selectOneMenu binding="#{cc.month}" id="month" styleClass="compositeMonth" disabled="#{cc.attrs.disabledBy}">
<f:selectItem itemLabel="Month" itemDisabled="true" />
<f:selectItems value="#{dropdownsBean.months(cc.attrs.limitFuture)}" var="item" itemValue="#{item.name}" itemLabel="#{item.customerDescription}" />
</h:selectOneMenu>
<h:inputText autocomplete="off" binding="#{cc.year}" id="year" maxlength="4" styleClass="compositeYear" disabled="#{cc.attrs.disabledBy}" a:placeholder="yyyy" a:pattern="[0-9]{4}" />
</div>
</cc:implementation>
用于表格:
<cc:compositeLocalDate id="dob" value="#{myBean.aList[formBean.selectedIndex].dateOfBirth}" days="true" />
表单会多次重复使用,以便向列表中添加更多项目。单击添加按钮时,表单将被清除:
protected void loadNewItem(MyBean myBean, FormBean formBean){
myBean.add(new Person());
formBean.reset();
formBean.setSelectedIndex(myBean.getAList.size()-1);
}
这是FormBean重置方法:
public void reset(){
name = null;
//other code that resets Members of FormBean
}
我遇到的问题是,在添加除复合组件之外的其他人时,一切都从表单中清除,复合组件保留为前一个人输入的值。我假设这是因为它们存储在CompositeLocalDate
java类:
public abstract class GenericCompositeLocalDate extends UIInput implements NamingContainer {
private UIInput day;
private UIInput month;
private UIInput year;
private static final Logger LOG = Logger.getLogger(GenericCompositeLocalDate.class);
@Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
if (getValue() != null && isValid()) {
Calendar cal = Calendar.getInstance();
cal.setTime(((LocalDate) getValue()).toDate());
month.setValue(String.valueOf(cal.get(Calendar.MONTH) + 1));
year.setValue(String.valueOf(cal.get(Calendar.YEAR)));
boolean checkDays = (Boolean) getAttributes().get("days");
if (checkDays) {
day.setValue(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
}
}
super.encodeBegin(context);
}
@Override
public Object getSubmittedValue() {
boolean checkDays = (Boolean) getAttributes().get("days");
return (checkDays ? day.getSubmittedValue() : "01") + "/" + month.getSubmittedValue() + "/" + year.getSubmittedValue();
}
@Override
protected Object getConvertedValue(FacesContext context, Object submittedValue) {
LOG.debug("investigate. submittedValue is \"" + submittedValue + "\"");
try {
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("dd/MM/yyyy");
format.setLenient(false);
return new LocalDate (format.parse((String) submittedValue));
} catch (ParseException e) {
return null;
}
}
public UIInput getDay() {
return day;
}
public void setDay(UIInput day) {
this.day = day;
}
public UIInput getMonth() {
return month;
}
public void setMonth(UIInput month) {
this.month = month;
}
public UIInput getYear() {
return year;
}
public void setYear(UIInput year) {
this.year = year;
}
}
如何清除这些值?