在一个 JSF 项目中,我有一个由字符串值列表填充的 SelectOneMenu,并连接到一个整数值“yearStart”。当我从列表中选择一个值时,转换器总是输出到控制台“stringVal = null”。为什么所选项目的值没有到达转换器?
我的网页包括以下内容:
<h:selectOneMenu value="#{applicantController.yearStart}">
<f:selectItems value="#{yearList.years}" />
<f:converter converterId="studyYearConverter"/>
</h:selectOneMenu>
它使用以下年份列表:
@ManagedBean
@ApplicationScoped
public class YearList implements Serializable {
private static final long serialVersionUID = 1L;
private final List<String> years = Arrays.asList("prior to 1990", "1990",
"1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998",
"1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006",
"2007", "2008", "2009", "2010", "2011", "2012", "2013");
public List<String> getYears() {
return years;
}
}
我的转换器类:
@FacesConverter(value = "studyYearConverter")
public class StudyYearConverter implements Converter {
private static final String PRIOR_TO_1990 = "prior to 1990";
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String stringVal) {
if (PRIOR_TO_1990.equals(stringVal)) {
return -1;
} else {
System.out.println("stringVal = " + stringVal);
// Why is this alway null??
try {
return Integer.parseInt(stringVal);
} catch (NumberFormatException pe) {
FacesMessage message = new FacesMessage(
"Invalid date format. Valid format e.g. 2010-04");
throw new ConverterException(message);
}
}
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
if (value == null || !(value instanceof Integer)) {
return null;
} else {
Integer year = (Integer) value;
if (year == -1) {
return PRIOR_TO_1990;
} else {
return year.toString();
}
}
}
}