I'm a little stuck with a selectOneMenu
.
I have a DB table foo
with a Bean and DAO as well as a POJO. I have already established a working CRUD page thanks to BalusC's great tutorial.
Foo
has the columns id
, text
, and a1
to a5
as further values.
The menu only displays a list of hash codes, like "com.example.beans.foo@3a3526c3", no matter what I put as itemValue
. I can put in blabla
as well, the result is the same.
Here's the JSF.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view><h:form>
<h:selectOneMenu value=#{fooBean.id}>
<f:selectItems value="#{fooBean.list}" var="foo" itemValue="#{foo.id}" itemLabel="#{foo.text}" />
</h:selectOneMenu>
</h:column>
</h:form>
</f:view>
</html>
And here's foo
:
@ManagedBean (name = "foo")
@ViewScoped
public class Foo implements Serializable {
private String text;
private String id;
//getters and setters
}
and fooBean
:
@ManagedBean (name="fooBean")
@ViewScoped
public class FooBean implements Serializable {
private List<Foo> list;
private Foo foo = new Foo();
private boolean edited;
private String id;
private String text;
@EJB
private FooDAO fooDAO;
@PostConstruct
public void init() {
list = fooDAO.getAll();
}
public void add() {
String id = fooDAO.insert(foo);
foo.setId(id);
list.add(foo);
foo = new Foo();
}
public void edit(Foo Foo) {
this.foo = Foo;
edited = true;
}
public void save() {
fooDAO.update(foo);
foo = new Foo();
edited = false;
}
public void delete(Foo Foo) {
fooDAO.delete(Foo);
list.remove(Foo);
}
public List<Foo> getList() {
return list;
}
public Foo getFoo() {
return foo;
}
public boolean isEdited() {
return edited;
}
public String getId() {
return id;
}
public String getText() {
return text;
}
}