0
我試圖從selectOneMenu
中獲得一個包含名字,姓氏等字段的對象。這是我的表格:@FacesConverter類中的@Inject PersonDao爲空
<h:form>
<p:outputLabel value="Persons: " />
<p:selectOneMenu value="#{personBean.person}">
<f:selectItem itemLabel="Select Person" itemValue="" noSelectionOption="true"/>
<f:selectItems itemLabel="#{person.firstName}" itemValue="#{person}" var="person" value="#{personBean.persons}" />
</p:selectOneMenu>
<br /><br />
<p:commandButton value="Submit"
action="#{personBean.showSomething()}" icon="ui-icon-check" />
我不明白..我在哪裏去了?我怎樣才能得到這個對象?
我一直在嘗試幾天,但我還沒有設法解決這個問題... 我試過使用Converter
,但我不斷得到NPE。
編輯
這是我的轉換器:(我想從我的Postgres獲取對象數據庫與我的DAO的幫助下)
@Named
@FacesConverter(forClass=Person.class)
public class PersonConverter implements Converter {
@Inject
private PersonDao personDao;
@Override
public Object getAsObject(FacesContext arg0, UIComponent component, String submittedValue) {
if (submittedValue == null || submittedValue.isEmpty()) {
return null;
}
try {
Person p = personDao.findById(Long.valueOf(submittedValue));
return p;
} catch (NumberFormatException e) {
throw new ConverterException(new FacesMessage(submittedValue + " is not a valid Person ID"), e);
}
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
if (arg2 == null) {
return "";
}
if (arg2 instanceof Person) {
return String.valueOf(((Person) arg2).getId());
} else {
throw new ConverterException(new FacesMessage(arg2 + " is not a valid Person"));
}
}
}
這是我得到一個NPE Person p = personDao.findById(Long.valueOf(submittedValue));
find()方法在其他地方工作... 當我使用調試器時,我注意到personDao爲空。我怎樣才能解決這個問題?
只有當我使用該轉換器。如果沒有,'personBean.showInfo()'甚至沒有被調用,所以它沒有返回任何東西。 – ffs
請添加更多信息。就像你的堆棧跟蹤你的NPE。另外,不應該itemValue是#{人}?而不是#{person.firstName}? – Fritz
謝謝指出! – ffs