在我的应用程序中,我有 selectOneMenu 来选择月份。
<h:selectOneMenu value="#{register.monthOfBirth}">
<f:selectItems value="#{register.dateValues.monthsList}" var="month" itemLabel="#{month.valueLabel}" itemValue="#{month.valueValue}"/>
<f:ajax listener="#{register.monthChanged}" render="messages" />
</h:selectOneMenu>
这是我用来填充 selectOneMenu 的 DateValues 类:
public class DateValues {
public static class Values{
public String valueLabel;
public String valueValue;
public Values(String valueLabel, String valueValue){
this.valueLabel = valueValue;
this.valueLabel = valueValue;
}
public String getValueLabel(){
return valueLabel;
}
public String getValueValue(){
return valueValue;
}
}
public Values[] daysList;
public Values[] monthsList;
public Values[] yearsList;
public DateValues(){
daysList = new Values[31];
for(int i=0;i<31;i++){
daysList[i] = new Values(""+ (i+1),""+(i+1));
}
monthsList = new Values[12];
for(int i=0;i<12;i++){
monthsList[i] = new Values("" + (i+1),"" + (i+1));
}
yearsList = new Values[109];
Calendar cal = Calendar.getInstance();
int currentYear = cal.get(Calendar.YEAR);
int first = currentYear - 108;
for(int i=0;i<109;i++) {
yearsList[i] = new Values("" + (first + (108-i)),"" + (first + (108-i)));
}
}
public Values[] getDaysList() {
return daysList;
}
public Values[] getMonthsList() {
return monthsList;
}
public Values[] getYearsList() {
return yearsList;
}
}
在 @ManagedBean 我有一个 ajax 监听器:
public void monthChanged(){
System.out.println("New month is month number " + monthOfBirth);
}
我在输出中得到的是:
"INFO: New month is month number dyplom.dyplom.date.DateValues$Values@79f6099"
而不是(例如)2(月数,所以是二月)
我做错什么了?
编辑:
生成的 HTML:
<h2><select id="j_idt56:j_idt70" name="j_idt56:j_idt70" size="1" onchange="mojarra.ab(this,event,'valueChange',0,'j_idt56:messages')"> <option value="dyplom.dyplom.date.DateValues$Values@32b0c37e">1</option>
<option value="dyplom.dyplom.date.DateValues$Values@72f29f31">2</option>
<option value="dyplom.dyplom.date.DateValues$Values@16b184ec">3</option>
<option value="dyplom.dyplom.date.DateValues$Values@608dad97">4</option>
<option value="dyplom.dyplom.date.DateValues$Values@757fc606">5</option>
<option value="dyplom.dyplom.date.DateValues$Values@17a1f02e">6</option>
<option value="dyplom.dyplom.date.DateValues$Values@89f6821">7</option>
<option value="dyplom.dyplom.date.DateValues$Values@196d52f3">8</option>
<option value="dyplom.dyplom.date.DateValues$Values@5860dc8f">9</option>
<option value="dyplom.dyplom.date.DateValues$Values@3f305d9d">10</option>
<option value="dyplom.dyplom.date.DateValues$Values@520ed4">11</option>
<option value="dyplom.dyplom.date.DateValues$Values@b4b8076">12</option>
</select>
</h2>