I am trying to get from selectOneMenu
an object that has fields such as first name, last name and so on. This is my form:
<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" />
I don't understand.. where am i going wrong? How can i get that object?
I've been trying for a few days but i haven't managed to fix this problem...
I tried using a Converter
but i kept getting NPEs.
EDIT
This is my converter:(I am trying to get the object from my postgres db with the help of my 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"));
}
}
}
This is where i get a NPE Person p = personDao.findById(Long.valueOf(submittedValue));
The find() method works anywhere else...
When i used the debugger i noticed that personDao is null. How can i fix this?